Skip to content

Commit

Permalink
Merge pull request #3 from ibks-bank/feature/BIP-17
Browse files Browse the repository at this point in the history
[BIP-17] userID in jwt
  • Loading branch information
ujuzy authored Apr 2, 2022
2 parents 5db605e + 64cd627 commit c4c20d9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
5 changes: 3 additions & 2 deletions auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
type userInfo struct {
Username string
Password string
UserID int64
}

func (a *authorizer) Interceptor(
Expand Down Expand Up @@ -59,12 +60,12 @@ func (a *authorizer) authorize(ctx context.Context) (context.Context, error) {

token := authHeader[0]

username, password, err := ParseToken(token, []byte(a.key))
username, password, userID, err := ParseToken(token, []byte(a.key))
if err != nil {
return ctx, status.Errorf(codes.Unauthenticated, err.Error())
}

return context.WithValue(ctx, UserKey, userInfo{Username: username, Password: password}), nil
return context.WithValue(ctx, UserKey, userInfo{Username: username, Password: password, UserID: userID}), nil
}

func GetUserInfo(ctx context.Context) (*userInfo, error) {
Expand Down
8 changes: 4 additions & 4 deletions auth/parse_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/dgrijalva/jwt-go/v4"
)

func ParseToken(accessToken string, signingKey []byte) (string, string, error) {
func ParseToken(accessToken string, signingKey []byte) (string, string, int64, error) {
token, err := jwt.ParseWithClaims(accessToken, &Claims{}, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
Expand All @@ -15,12 +15,12 @@ func ParseToken(accessToken string, signingKey []byte) (string, string, error) {
})

if err != nil {
return "", "", err
return "", "", 0, err
}

if claims, ok := token.Claims.(*Claims); ok && token.Valid {
return claims.Username, claims.Password, nil
return claims.Username, claims.Password, claims.UserID, nil
}

return "", "", ErrInvalidAccessToken
return "", "", 0, ErrInvalidAccessToken
}
4 changes: 3 additions & 1 deletion auth/sign_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Claims struct {
jwt.StandardClaims
Username string `json:"username"`
Password string `json:"password"`
UserID int64 `json:"user_id"`
}

type authorizer struct {
Expand All @@ -23,14 +24,15 @@ func NewAuthorizer(key string, expireDuration time.Duration) *authorizer {
return &authorizer{key: key, expireDuration: expireDuration}
}

func (a *authorizer) GetToken(login, password, salt string) (string, error) {
func (a *authorizer) GetToken(login, password, salt string, userID int64) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, &Claims{
StandardClaims: jwt.StandardClaims{
ExpiresAt: jwt.At(time.Now().Add(a.expireDuration)),
IssuedAt: jwt.At(time.Now()),
},
Username: login,
Password: HashPassword(password, salt),
UserID: userID,
})

return token.SignedString([]byte(a.key))
Expand Down

0 comments on commit c4c20d9

Please sign in to comment.