Merge pull request #598 from bradrydzewski/master

minor session refactoring in prep for upcoming changes discussed in #590
This commit is contained in:
Brad Rydzewski 2014-10-20 00:07:29 -07:00
commit dfb0210cf5

View file

@ -52,11 +52,11 @@ func GenerateToken(c context.Context, r *http.Request, user *model.User) (string
// auth token. // auth token.
func getUserToken(c context.Context, r *http.Request) *model.User { func getUserToken(c context.Context, r *http.Request) *model.User {
var token = r.FormValue("access_token") var token = r.FormValue("access_token")
var user = getUserJwtToken(c, token) var user = getUserJWT(c, token)
// TODO: is it needed to fallback to user_token query? if user != nil {
if user == nil { return user
user, _ = datastore.GetUserToken(c, token)
} }
user, _ = datastore.GetUserToken(c, token)
return user return user
} }
@ -65,20 +65,19 @@ func getUserToken(c context.Context, r *http.Request) *model.User {
func getUserBearer(c context.Context, r *http.Request) *model.User { func getUserBearer(c context.Context, r *http.Request) *model.User {
var tokenstr = r.Header.Get("Authorization") var tokenstr = r.Header.Get("Authorization")
fmt.Sscanf(tokenstr, "Bearer %s", &tokenstr) fmt.Sscanf(tokenstr, "Bearer %s", &tokenstr)
var user = getUserJwtToken(c, tokenstr) return getUserJWT(c, tokenstr)
return user
} }
// getUserAccessToken gets the currently authenticated user for the given // getUserJWT is a helper function that parses the User ID
// auth token in jwt format. // and retrieves the User data from a JWT Token.
func getUserJwtToken(c context.Context, tokenstr string) *model.User { func getUserJWT(c context.Context, token string) *model.User {
var token, err = jwt.Parse(tokenstr, func(t *jwt.Token) (interface{}, error) { var t, err = jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
return []byte(*secret), nil return []byte(*secret), nil
}) })
if err != nil || !token.Valid { if err != nil || !t.Valid {
return nil return nil
} }
var userid, ok = token.Claims["user_id"].(float64) var userid, ok = t.Claims["user_id"].(float64)
if !ok { if !ok {
return nil return nil
} }