Initial commit
This commit is contained in:
31
pkg/auth/jwt.go
Normal file
31
pkg/auth/jwt.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/golang-jwt/jwt"
|
||||
)
|
||||
|
||||
// Auth struct represents parsed jwt information.
|
||||
type Auth struct {
|
||||
UID string `json:"uid"`
|
||||
State string `json:"state"`
|
||||
Email string `json:"email"`
|
||||
Role string `json:"role"`
|
||||
ReferralID json.Number `json:"referral_id"`
|
||||
Level json.Number `json:"level"`
|
||||
Audience []string `json:"aud,omitempty"`
|
||||
jwt.StandardClaims
|
||||
}
|
||||
|
||||
// ParseAndValidate parses token and validates RS256 signature with Barong RSA public key.
|
||||
func ParseAndValidate(token string, key *rsa.PublicKey) (Auth, error) {
|
||||
auth := Auth{}
|
||||
|
||||
_, err := jwt.ParseWithClaims(token, &auth, func(t *jwt.Token) (interface{}, error) {
|
||||
return key, nil
|
||||
})
|
||||
|
||||
return auth, err
|
||||
}
|
||||
Reference in New Issue
Block a user