Skip to content
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

Using RegisteredClaims in JWT to avoid deprecated code #503

Merged
merged 1 commit into from
Sep 9, 2024
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
4 changes: 2 additions & 2 deletions admin/handlers/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ func (h *HandlersAdmin) UsersPOSTHandler(w http.ResponseWriter, r *http.Request)
return
}
if u.Token {
token, exp, err := h.Users.CreateToken(newUser.Username)
token, exp, err := h.Users.CreateToken(newUser.Username, h.AdminConfig.Host)
if err != nil {
adminErrorResponse(w, "error creating token", http.StatusInternalServerError, err)
h.Inc(metricAdminErr)
Expand Down Expand Up @@ -1348,7 +1348,7 @@ func (h *HandlersAdmin) UsersPOSTHandler(w http.ResponseWriter, r *http.Request)
return
}
*/
token, exp, err := h.Users.CreateToken(u.Username)
token, exp, err := h.Users.CreateToken(u.Username, h.AdminConfig.Host)
if err != nil {
adminErrorResponse(w, "error creating token", http.StatusInternalServerError, err)
h.Inc(metricAdminErr)
Expand Down
2 changes: 1 addition & 1 deletion admin/handlers/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (h *HandlersAdmin) TokensPOSTHandler(w http.ResponseWriter, r *http.Request
if h.Settings.DebugService(settings.ServiceAdmin) {
log.Println("DebugService: Creating token")
}
token, exp, err := h.Users.CreateToken(user.Username)
token, exp, err := h.Users.CreateToken(user.Username, h.AdminConfig.Host)
if err != nil {
adminErrorResponse(w, "error creating token", http.StatusInternalServerError, err)
h.Inc(metricAdminErr)
Expand Down
2 changes: 1 addition & 1 deletion api/handlers-login.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func apiLoginHandler(w http.ResponseWriter, r *http.Request) {
}
// Do we have a token already?
if user.APIToken == "" {
token, exp, err := apiUsers.CreateToken(l.Username)
token, exp, err := apiUsers.CreateToken(l.Username, serviceName)
if err != nil {
apiErrorResponse(w, "error creating token", http.StatusInternalServerError, err)
incMetric(metricAPILoginErr)
Expand Down
23 changes: 11 additions & 12 deletions users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type AdminUser struct {
// TokenClaims to hold user claims when using JWT
type TokenClaims struct {
Username string `json:"username"`
jwt.StandardClaims
jwt.RegisteredClaims
}

// UserManager have all users of the system
Expand Down Expand Up @@ -100,15 +100,14 @@ func (m *UserManager) CheckLoginCredentials(username, password string) (bool, Ad
}

// CreateToken to create a new JWT token for a given user
func (m *UserManager) CreateToken(username string) (string, time.Time, error) {
func (m *UserManager) CreateToken(username, issuer string) (string, time.Time, error) {
expirationTime := time.Now().Add(time.Hour * time.Duration(m.JWTConfig.HoursToExpire))
// Create the JWT claims, which includes the username, level and expiry time
claims := &TokenClaims{
Username: username,
StandardClaims: jwt.StandardClaims{
// In JWT, the expiry time is expressed as unix milliseconds
ExpiresAt: expirationTime.Unix(),
Issuer: DefaultTokeIssuer,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(expirationTime),
Issuer: issuer,
},
}
// Declare the token with the algorithm used for signing, and the claims
Expand Down Expand Up @@ -163,12 +162,12 @@ func (m *UserManager) New(username, password, email, fullname string, admin bool
return AdminUser{}, err
}
return AdminUser{
Username: username,
PassHash: passhash,
UUID: utils.GenUUID(),
Admin: admin,
Email: email,
Fullname: fullname,
Username: username,
PassHash: passhash,
UUID: utils.GenUUID(),
Admin: admin,
Email: email,
Fullname: fullname,
}, nil
}
return AdminUser{}, fmt.Errorf("%s already exists", username)
Expand Down
2 changes: 1 addition & 1 deletion users/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestUserManager(t *testing.T) {
assert.Equal(t, 123, int(user.EnvironmentID))
})
t.Run("CreateCheckToken", func(t *testing.T) {
token, tt, err := manager.CreateToken("testUsername")
token, tt, err := manager.CreateToken("testUsername", "issuer")
assert.NoError(t, err)
assert.NotEmpty(t, token)
now := time.Now()
Expand Down
Loading