mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-27 19:00:35 +00:00
minor session refactoring in prep for upcoming changes discussed in #590
This commit is contained in:
parent
72e8f80f5e
commit
07e0b7d2b1
1 changed files with 11 additions and 12 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue