32 lines
801 B
Go
32 lines
801 B
Go
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
|
|
}
|