Skip to content

Commit

Permalink
update management api regarding refactored jwt token service
Browse files Browse the repository at this point in the history
  • Loading branch information
erudenko committed Jul 12, 2023
1 parent 35906d9 commit 53f4fc8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 14 deletions.
2 changes: 1 addition & 1 deletion jwt/service/jwt_token_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (ts *JWTokenService) Parse(s string) (model.JWToken, error) {
}

// ValidateTokenString parses token and validates it.
func (ts *JWTokenService) ValidateTokenString(tstr string, v jv.Validator, tokenType string) (model.JWToken, error) {
func (ts *JWTokenService) ValidateTokenString(tstr string, v jv.Validator, tokenType model.TokenType) (model.JWToken, error) {
token, err := ts.Parse(tstr)
if err != nil {
return model.JWToken{}, err
Expand Down
28 changes: 28 additions & 0 deletions jwt/service/jwt_token_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,31 @@ func TestNewToken(t *testing.T) {
_, err = tokenService.Parse(tokenString)
require.NoError(t, err)
}

func TestNewResetToken(t *testing.T) {
tokenService := createTokenService(t)

user := model.User{
ID: "12345566",
Username: "username",
Email: "username@gmailc.om",
}
token, err := tokenService.NewToken(model.TokenTypeReset, user, nil, nil, nil)
assert.NoError(t, err)

ts, err := tokenService.SignToken(token)
assert.NoError(t, err)

pt, err := tokenService.Parse(ts)
require.NoError(t, err)
assert.Equal(t, model.TokenTypeReset, pt.Type())

// check we have empty audience
aud, err := pt.Claims.GetAudience()
assert.NoError(t, err)
assert.Empty(t, aud)

// check we have empty email and username
assert.NotContains(t, pt.Payload(), "email")
assert.NotContains(t, pt.Payload(), "username")
}
6 changes: 3 additions & 3 deletions model/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (t *JWToken) UserID() string {
}

// Payload returns token payload.
func (t *JWToken) Payload() map[string]interface{} {
func (t *JWToken) Payload() map[string]any {
claims, ok := t.Claims.(*Claims)
if !ok {
return nil
Expand All @@ -86,12 +86,12 @@ func (t *JWToken) Payload() map[string]interface{} {
}

// Type returns token type.
func (t *JWToken) Type() string {
func (t *JWToken) Type() TokenType {
claims, ok := t.Claims.(*Claims)
if !ok {
return ""
}
return claims.Type
return TokenType(claims.Type)
}

// ExpiresAt standard token claim
Expand Down
25 changes: 24 additions & 1 deletion web/management/invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@ import (

"github.com/madappgang/identifo/v2/l"
"github.com/madappgang/identifo/v2/model"
"golang.org/x/exp/maps"
)

// InviteRequest is a request for invite.
type InvitationTokenRequest struct {
Email string `json:"email"`
AppID string `json:"app_id"`
Roles map[string]any `json:"roles"`
CallbackURL string `json:"callback"`
Data map[string]any `json:"data"`
}

func (ar *Router) getInviteToken(w http.ResponseWriter, r *http.Request) {
locale := r.Header.Get("Accept-Language")

Expand All @@ -33,7 +43,20 @@ func (ar *Router) getInviteToken(w http.ResponseWriter, r *http.Request) {
return
}

inviteToken, err := ar.server.Services().Token.NewInviteToken(d.Email, d.Role, d.ApplicationID, d.Data)
u := model.User{
ID: model.NewUserID.String(), // token sub is new user ID
Email: d.Email,
}
aud := []string{}
if len(d.AppID) > 0 {
aud = append(aud, d.AppID)
}
fields := model.UserFieldsetMap[model.UserFieldsetInviteToken]
maps.Copy(d.Data, d.Roles)
if len(d.CallbackURL) > 0 {
d.Data["callback"] = d.CallbackURL
}
inviteToken, err := ar.server.Services().Token.NewToken(model.TokenTypeInvite, u, aud, fields, d.Data)
if err != nil {
ar.Error(w, locale, http.StatusInternalServerError, l.ErrorTokenInviteCreateError, err)
return
Expand Down
8 changes: 0 additions & 8 deletions web/management/models.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package management

type InvitationTokenRequest struct {
Email string `json:"email"`
ApplicationID string `json:"application_id"`
Role string `json:"access_role"`
CallbackURL string `json:"callback_url"`
Data map[string]interface{} `json:"data"`
}

type ResetPasswordTokenRequest struct {
Email string `json:"email"`
}
3 changes: 2 additions & 1 deletion web/management/reset_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func (ar *Router) getResetPasswordToken(w http.ResponseWriter, r *http.Request)
return
}

resetToken, err := ar.server.Services().Token.NewResetToken(user.ID)
// reset token has no auditory, fields from user or any other payload
resetToken, err := ar.server.Services().Token.NewToken(model.TokenTypeReset, user, nil, nil, nil)
if err != nil {
ar.Error(w, locale, http.StatusInternalServerError, l.ErrorTokenUnableToCreateResetTokenError, err)
return
Expand Down

0 comments on commit 53f4fc8

Please sign in to comment.