Skip to content

Refactor jwt.StandardClaims to RegisteredClaims #18344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cmd/serv.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,9 @@ func runServ(c *cli.Context) error {

now := time.Now()
claims := lfs.Claims{
// FIXME: we need to migrate to RegisteredClaims
StandardClaims: jwt.StandardClaims{ // nolint
ExpiresAt: now.Add(setting.LFS.HTTPAuthExpiry).Unix(),
NotBefore: now.Unix(),
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(now.Add(setting.LFS.HTTPAuthExpiry)),
NotBefore: jwt.NewNumericDate(now),
},
RepoID: results.RepoID,
Op: lfsVerb,
Expand Down
24 changes: 10 additions & 14 deletions routers/web/auth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,8 @@ func newAccessTokenResponse(grant *auth.OAuth2Grant, serverKey, clientKey oauth2
accessToken := &oauth2.Token{
GrantID: grant.ID,
Type: oauth2.TypeAccessToken,
// FIXME: Migrate to RegisteredClaims
StandardClaims: jwt.StandardClaims{ //nolint
ExpiresAt: expirationDate.AsTime().Unix(),
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(expirationDate.AsTime()),
},
}
signedAccessToken, err := accessToken.SignToken(serverKey)
Expand All @@ -163,14 +162,13 @@ func newAccessTokenResponse(grant *auth.OAuth2Grant, serverKey, clientKey oauth2
}

// generate refresh token to request an access token after it expired later
refreshExpirationDate := timeutil.TimeStampNow().Add(setting.OAuth2.RefreshTokenExpirationTime * 60 * 60).AsTime().Unix()
refreshExpirationDate := timeutil.TimeStampNow().Add(setting.OAuth2.RefreshTokenExpirationTime * 60 * 60).AsTime()
refreshToken := &oauth2.Token{
GrantID: grant.ID,
Counter: grant.Counter,
Type: oauth2.TypeRefreshToken,
// FIXME: Migrate to RegisteredClaims
StandardClaims: jwt.StandardClaims{ // nolint
ExpiresAt: refreshExpirationDate,
RegisteredClaims: jwt.RegisteredClaims{ // nolint
ExpiresAt: jwt.NewNumericDate(refreshExpirationDate),
},
}
signedRefreshToken, err := refreshToken.SignToken(serverKey)
Expand Down Expand Up @@ -207,11 +205,10 @@ func newAccessTokenResponse(grant *auth.OAuth2Grant, serverKey, clientKey oauth2
}

idToken := &oauth2.OIDCToken{
// FIXME: migrate to RegisteredClaims
StandardClaims: jwt.StandardClaims{ //nolint
ExpiresAt: expirationDate.AsTime().Unix(),
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(expirationDate.AsTime()),
Issuer: setting.AppURL,
Audience: app.ClientID,
Audience: []string{app.ClientID},
Subject: fmt.Sprint(grant.UserID),
},
Nonce: grant.Nonce,
Expand Down Expand Up @@ -329,8 +326,7 @@ func IntrospectOAuth(ctx *context.Context) {
var response struct {
Active bool `json:"active"`
Scope string `json:"scope,omitempty"`
// FIXME: Migrate to RegisteredClaims
jwt.StandardClaims //nolint
jwt.RegisteredClaims
}

form := web.GetForm(ctx).(*forms.IntrospectTokenForm)
Expand All @@ -344,7 +340,7 @@ func IntrospectOAuth(ctx *context.Context) {
response.Active = true
response.Scope = grant.Scope
response.Issuer = setting.AppURL
response.Audience = app.ClientID
response.Audience = []string{app.ClientID}
response.Subject = fmt.Sprint(grant.UserID)
}
}
Expand Down
2 changes: 1 addition & 1 deletion services/auth/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func CheckOAuthAccessToken(accessToken string) int64 {
if token.Type != oauth2.TypeAccessToken {
return 0
}
if token.ExpiresAt < time.Now().Unix() || token.IssuedAt > time.Now().Unix() {
if token.ExpiresAt.Before(time.Now()) || token.IssuedAt.After(time.Now()) {
return 0
}
return grant.UserID
Expand Down
10 changes: 4 additions & 6 deletions services/auth/source/oauth2/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ type Token struct {
GrantID int64 `json:"gnt"`
Type TokenType `json:"tt"`
Counter int64 `json:"cnt,omitempty"`
// FIXME: Migrate to registered claims
jwt.StandardClaims
jwt.RegisteredClaims
}

// ParseToken parses a signed jwt string
Expand All @@ -62,16 +61,15 @@ func ParseToken(jwtToken string, signingKey JWTSigningKey) (*Token, error) {

// SignToken signs the token with the JWT secret
func (token *Token) SignToken(signingKey JWTSigningKey) (string, error) {
token.IssuedAt = time.Now().Unix()
token.IssuedAt = jwt.NewNumericDate(time.Now())
jwtToken := jwt.NewWithClaims(signingKey.SigningMethod(), token)
signingKey.PreProcessToken(jwtToken)
return jwtToken.SignedString(signingKey.SignKey())
}

// OIDCToken represents an OpenID Connect id_token
type OIDCToken struct {
// FIXME: Migrate to RegisteredClaims
jwt.StandardClaims
jwt.RegisteredClaims
Nonce string `json:"nonce,omitempty"`

// Scope profile
Expand All @@ -93,7 +91,7 @@ type OIDCToken struct {

// SignToken signs an id_token with the (symmetric) client secret key
func (token *OIDCToken) SignToken(signingKey JWTSigningKey) (string, error) {
token.IssuedAt = time.Now().Unix()
token.IssuedAt = jwt.NewNumericDate(time.Now())
jwtToken := jwt.NewWithClaims(signingKey.SigningMethod(), token)
signingKey.PreProcessToken(jwtToken)
return jwtToken.SignedString(signingKey.SignKey())
Expand Down
3 changes: 1 addition & 2 deletions services/lfs/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ type Claims struct {
RepoID int64
Op string
UserID int64
// FIXME: Migrate to RegisteredClaims
jwt.StandardClaims
jwt.RegisteredClaims
}

// DownloadLink builds a URL to download the object.
Expand Down