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) } }
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) }