示例#1
0
文件: citizens.go 项目: quid-city/api
func (cz *Citizens) Login(auth *osin.Server) gin.HandlerFunc {
	return func(c *gin.Context) {
		// see https://github.com/RangelReale/osin/blob/master/example/complete/complete.go#L45
		res := auth.NewResponse()

		if aReq := auth.HandleAccessRequest(res, c.Request); aReq != nil {
			// check username/password
			user := &models.User{}
			// find in DB
			err := cz.Connection.Collection(COL_CITIZEN).FindOne(bson.M{"username": aReq.Username}, user)

			// user found and has valid username/password
			if err == nil && user.ValidCredentials(aReq.Username, aReq.Password) {
				aReq.Authorized = true
				// save user data along with the access token
				aReq.UserData = gin.H{"username": user.UserName}
			}
			// creates the response automatically with error message and code
			auth.FinishAccessRequest(res, c.Request, aReq)
		}

		if res.IsError && res.InternalError != nil {
			fmt.Printf("ACCESS_ERROR: %s\n", res.InternalError)
		}

		osin.OutputJSON(res, c.Writer, c.Request)
	}
}
示例#2
0
func GET_info(w http.ResponseWriter, r *http.Request, s *osin.Server) {
	resp := s.NewResponse()
	if ir := s.HandleInfoRequest(resp, r); ir != nil {
		s.FinishInfoRequest(resp, r, ir)
	}
	osin.OutputJSON(resp, w, r)
}
示例#3
0
func GET_token(w http.ResponseWriter, r *http.Request, s *osin.Server) {
	resp := s.NewResponse()
	if ar := s.HandleAccessRequest(resp, r); ar != nil {
		// always true
		ar.Authorized = true
		s.FinishAccessRequest(resp, r, ar)
	}
	osin.OutputJSON(resp, w, r)
}
示例#4
0
func GET_authorize(c martini.Context, sess sessions.Session, w http.ResponseWriter, r *http.Request, s *osin.Server) {
	resp := s.NewResponse()
	if ar := s.HandleAuthorizeRequest(resp, r); ar != nil {
		if !inner_GET_authorize(c, sess, r, ar) {
			return
		}

		ar.Authorized = true
		s.FinishAuthorizeRequest(resp, r, ar)
	}
	osin.OutputJSON(resp, w, r)
}