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

Added username claim #8

Merged
merged 2 commits into from
Aug 26, 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/mysteriumnetwork/token
module github.com/mysteriumnetwork/token/v2

go 1.18

Expand Down
3 changes: 2 additions & 1 deletion issuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const (
)

// Issue will issue a new JWT token setting given parameters inside the claims.
func (j *IssuerJWT) Issue(sub string, aud []string, issuertype string, ttl time.Duration, attr string) (string, error) {
func (j *IssuerJWT) Issue(sub string, aud []string, issuertype string, ttl time.Duration, attr string, username string) (string, error) {
if sub == "" || len(aud) < 1 || issuertype == "" {
return "", errors.New("'sub', 'aud', 'issuertype' claims must be set")
}
Expand All @@ -55,6 +55,7 @@ func (j *IssuerJWT) Issue(sub string, aud []string, issuertype string, ttl time.
"sub": sub,
"attr": attr,
"jti": id.String(),
"username": username,
CustomClaimIssuerType: issuertype,
}

Expand Down
1 change: 1 addition & 0 deletions jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ type ClaimData struct {
ExpiresAt float64
Attributes string
ID string
Username string
}
9 changes: 5 additions & 4 deletions jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Test_JWT(t *testing.T) {
aud := []string{"pool1"}
typ := "password"
sub := uuid.New().String()
token, err := jwt.Issue(sub, aud, typ, time.Hour, "")
token, err := jwt.Issue(sub, aud, typ, time.Hour, "", "username1")
assert.NoError(t, err)

err = jwt.Validate(token)
Expand All @@ -58,7 +58,7 @@ func Test_JWT(t *testing.T) {
aud := []string{"pool1", "pool2"}
typ := "password"
sub := uuid.New().String()
token, err := jwt.Issue(sub, aud, typ, time.Hour, "")
token, err := jwt.Issue(sub, aud, typ, time.Hour, "", "username1")
assert.NoError(t, err)

err = jwt.ValidateForAudience(token, "pool1")
Expand All @@ -73,7 +73,7 @@ func Test_JWT(t *testing.T) {
aud := []string{"pool1"}
typ := "password"
sub := uuid.New().String()
token, err := jwt.Issue(sub, aud, typ, time.Hour, "")
token, err := jwt.Issue(sub, aud, typ, time.Hour, "", "username1")
assert.NoError(t, err)

d, err := jwt.ValidateExtract(token)
Expand All @@ -82,6 +82,7 @@ func Test_JWT(t *testing.T) {
assert.Equal(t, sub, d.Subject)
assert.Equal(t, IssuerName, d.Issuer)
assert.Equal(t, typ, d.IssuerType)
assert.Equal(t, "username1", d.Username)
_, err = jwt.ValidateForAudienceExtract(token, "pool2")
assert.Error(t, err)
d, err = jwt.ValidateForAudienceExtract(token, aud[0])
Expand All @@ -100,7 +101,7 @@ func Test_JWT(t *testing.T) {
aud := []string{"pool1"}
typ := "password"
sub := uuid.New().String()
token, err := jwt.Issue(sub, aud, typ, time.Millisecond, "")
token, err := jwt.Issue(sub, aud, typ, time.Millisecond, "", "username1")
assert.NoError(t, err)

time.Sleep(10 * time.Millisecond)
Expand Down
2 changes: 2 additions & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (j *ValidatorJWT) validate(token string, aud *string) (*ClaimData, error) {
exp, _ := claims["exp"].(float64)
iat, _ := claims["iat"].(float64)
jti, _ := claims["jti"].(string)
username, _ := claims["username"].(string)
isst, _ := claims[CustomClaimIssuerType].(string)

return &ClaimData{
Expand All @@ -119,6 +120,7 @@ func (j *ValidatorJWT) validate(token string, aud *string) (*ClaimData, error) {
Attributes: attr,
ID: jti,
IssuedAt: iat,
Username: username,
}, nil
}

Expand Down
Loading