當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Context.Written方法代碼示例

本文整理匯總了Golang中github.com/go-martini/martini.Context.Written方法的典型用法代碼示例。如果您正苦於以下問題:Golang Context.Written方法的具體用法?Golang Context.Written怎麽用?Golang Context.Written使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/go-martini/martini.Context的用法示例。


在下文中一共展示了Context.Written方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: AuthMiddleware

//
// Middleware called every call that requires user Authentication
// It injects the Auth object in handlers that require it to work
// Return the user in this context or an anonymous user and an error
// Should be used .Logged to identify if it's an anonymous user
// and use .GetUser to get this context user or the anonymous user the error object
//
func AuthMiddleware(c martini.Context, db DB, r render.Render, req *http.Request) {
	anonymous := &User{
		UserId:     "anonymous",
		UserName:   "Anonimo",
		PicId:      "default",
		FullName:   "Usuario Anonimo",
		LikeCount:  0,
		Creation:   time.Now(),
		LastUpdate: time.Now(),
		Deleted:    false,
		Admin:      false,
	}

	// Try to get the credentials from Authorization header or credentials cookie
	credentials := req.Header.Get("Authorization")
	if credentials == "" {
		cookie, err := req.Cookie("credentials")
		if err == nil {
			credentials = cookie.Value
		}
	}

	if credentials == "" {
		err := errors.New("Voce nao apresentou credenciais de autorizacao")
		userAuth := &UserAuth{anonymous, err}
		c.MapTo(userAuth, (*Auth)(nil))
		c.Next()          // Call the next handler
		if !c.Written() { // Handler aborted
			// If the nex handler aborted, by an empty return
			// So he can't go on without user, let's alert user
			AccessDeniedHandler(r, req, err)
		}
		return
	}

	// Len of our 41 (20:20) char encoded in base64 is 56
	// (C++) long base64EncodedSize = 4 * (int)Math.Ceiling(originalSizeInBytes / 3.0);
	if len(credentials) != 56 {
		err := errors.New("Credenciais de authorizacao apresentadas sao invalidas.")
		userAuth := &UserAuth{anonymous, err}
		c.MapTo(userAuth, (*Auth)(nil))
		c.Next()          // Call the next handler
		if !c.Written() { // Handler aborted
			AccessDeniedHandler(r, req, err)
		}
		return
	}

	token, err := decodeAuth(credentials)
	if err != nil {
		userAuth := &UserAuth{anonymous, err}
		c.MapTo(userAuth, (*Auth)(nil))
		c.Next()          // Call the next handler
		if !c.Written() { // Handler aborted
			AccessDeniedHandler(r, req, err)
		}
		return
	}

	obj, err := db.Get(Token{}, token.TokenId, token.UserId)
	if err != nil {
		userAuth := &UserAuth{anonymous, err}
		c.MapTo(userAuth, (*Auth)(nil))
		c.Next()          // Call the next handler
		if !c.Written() { // Handler aborted
			AccessDeniedHandler(r, req, err)
		}
		return
	}

	if obj == nil {
		// This tokenid was not found
		err = errors.New("Credenciais fornecidas nao foram encontradas")
		userAuth := &UserAuth{anonymous, err}
		c.MapTo(userAuth, (*Auth)(nil))
		c.Next()          // Call the next handler
		if !c.Written() { // Handler aborted
			AccessDeniedHandler(r, req, err)
		}
		return
	}

	token = obj.(*Token)

	obj, err = db.Get(User{}, token.UserId)
	if err != nil {
		userAuth := &UserAuth{anonymous, err}
		c.MapTo(userAuth, (*Auth)(nil))
		c.Next()          // Call the next handler
		if !c.Written() { // Handler aborted
			AccessDeniedHandler(r, req, err)
		}
		return
//.........這裏部分代碼省略.........
開發者ID:rafadev7,項目名稱:server,代碼行數:101,代碼來源:auth.go

示例2: AuthMiddleware

func AuthMiddleware(c martini.Context, db DB, r render.Render, req *http.Request) {
	header := req.Header.Get("Authorization")
	if header == "" {
		err := errors.New("Voce nao apresentou credenciais de autorizacao")
		userAuth := &UserAuth{nil, err}
		c.MapTo(userAuth, (*Auth)(nil))
		c.Next() // Call the next handler
		if !c.Written() {
			// If the nex handler aborted, empty return
			// So he can't go on without user, let's alert user
			AccessDeniedHandler(r, req, err)
		}
		return
	}

	// Len of our 41 (20:20) char encoded in base64 is 56
	// (C++) long base64EncodedSize = 4 * (int)Math.Ceiling(originalSizeInBytes / 3.0);
	if len(header) < 56 {
		err := errors.New("Credenciais de authorizacao apresentadas sao invalidas.")
		userAuth := &UserAuth{nil, err}
		c.MapTo(userAuth, (*Auth)(nil))
		c.Next()          // Call the next handler
		if !c.Written() { // Response the user
			AccessDeniedHandler(r, req, err)
		}
		return
	}

	auth, err := decodeAuth(header)
	if err != nil {
		userAuth := &UserAuth{nil, err}
		c.MapTo(userAuth, (*Auth)(nil))
		c.Next()          // Call the next handler
		if !c.Written() { // Response the user
			AccessDeniedHandler(r, req, err)
		}
		return
	}

	i := strings.Index(auth, ":")
	if i == -1 {
		err = errors.New("Credenciais de authorizacao estao corrompidas")
		userAuth := &UserAuth{nil, err}
		c.MapTo(userAuth, (*Auth)(nil))
		c.Next()          // Call the next handler
		if !c.Written() { // Response the user
			AccessDeniedHandler(r, req, err)
		}
		return
	}

	tokenid := auth[:i]
	userid := auth[i+1:]

	obj, err := db.Get(Token{}, tokenid)
	if err != nil {
		userAuth := &UserAuth{nil, err}
		c.MapTo(userAuth, (*Auth)(nil))
		c.Next()          // Call the next handler
		if !c.Written() { // Response the user
			AccessDeniedHandler(r, req, err)
		}
		return
	}

	if obj == nil {
		// This tokenid was not found
		err = errors.New("Credenciais fornecidas nao foram encontradas")
		userAuth := &UserAuth{nil, err}
		c.MapTo(userAuth, (*Auth)(nil))
		c.Next()          // Call the next handler
		if !c.Written() { // Response the user
			AccessDeniedHandler(r, req, err)
		}
		return
	}

	token := obj.(*Token)
	if token.UserId != userid {
		err = errors.New("Usuario nao identificado pelas credenciais fornecidas")
		userAuth := &UserAuth{nil, err}
		c.MapTo(userAuth, (*Auth)(nil))
		c.Next()          // Call the next handler
		if !c.Written() { // Response the user
			AccessDeniedHandler(r, req, err)
		}
		return
	}

	obj, err = db.Get(User{}, userid)
	if err != nil {
		userAuth := &UserAuth{nil, err}
		c.MapTo(userAuth, (*Auth)(nil))
		c.Next()          // Call the next handler
		if !c.Written() { // Response the user
			AccessDeniedHandler(r, req, err)
		}
		return
	}

//.........這裏部分代碼省略.........
開發者ID:rafadev7,項目名稱:irado,代碼行數:101,代碼來源:auth.go


注:本文中的github.com/go-martini/martini.Context.Written方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。