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