refactor auth pkg into libraries
This commit is contained in:
parent
521f081ba0
commit
aaf0a0928d
26 changed files with 592 additions and 504 deletions
59
auth/jwt/claims.go
Normal file
59
auth/jwt/claims.go
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
package jwt
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/go-chi/jwtauth"
|
||||
)
|
||||
|
||||
// AppClaims represent the claims parsed from JWT access token.
|
||||
type AppClaims struct {
|
||||
ID int
|
||||
Sub string
|
||||
Roles []string
|
||||
}
|
||||
|
||||
// ParseClaims parses JWT claims into AppClaims.
|
||||
func (c *AppClaims) ParseClaims(claims jwtauth.Claims) error {
|
||||
id, ok := claims.Get("id")
|
||||
if !ok {
|
||||
return errors.New("could not parse claim id")
|
||||
}
|
||||
c.ID = int(id.(float64))
|
||||
|
||||
sub, ok := claims.Get("sub")
|
||||
if !ok {
|
||||
return errors.New("could not parse claim sub")
|
||||
}
|
||||
c.Sub = sub.(string)
|
||||
|
||||
rl, ok := claims.Get("roles")
|
||||
if !ok {
|
||||
return errors.New("could not parse claims roles")
|
||||
}
|
||||
|
||||
var roles []string
|
||||
if rl != nil {
|
||||
for _, v := range rl.([]interface{}) {
|
||||
roles = append(roles, v.(string))
|
||||
}
|
||||
}
|
||||
c.Roles = roles
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RefreshClaims represent the claims parsed from JWT refresh token.
|
||||
type RefreshClaims struct {
|
||||
Token string
|
||||
}
|
||||
|
||||
// ParseClaims parses the JWT claims into RefreshClaims.
|
||||
func (c *RefreshClaims) ParseClaims(claims jwtauth.Claims) error {
|
||||
token, ok := claims.Get("token")
|
||||
if !ok {
|
||||
return errors.New("could not parse claim token")
|
||||
}
|
||||
c.Token = token.(string)
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue