netbird/management/server/jwtclaims/extractor.go

105 lines
3.0 KiB
Go
Raw Normal View History

package jwtclaims
import (
"net/http"
"github.com/golang-jwt/jwt"
)
const (
2023-03-30 17:32:44 +02:00
// TokenUserProperty key for the user property in the request context
2023-03-30 18:54:55 +02:00
TokenUserProperty = "user"
2023-03-30 17:32:44 +02:00
// AccountIDSuffix suffix for the account id claim
2023-03-30 18:54:55 +02:00
AccountIDSuffix = "wt_account_id"
2023-03-30 17:32:44 +02:00
// DomainIDSuffix suffix for the domain id claim
2023-03-30 18:54:55 +02:00
DomainIDSuffix = "wt_account_domain"
2023-03-30 17:32:44 +02:00
// DomainCategorySuffix suffix for the domain category claim
2023-03-30 18:54:55 +02:00
DomainCategorySuffix = "wt_account_domain_category"
2023-03-30 17:32:44 +02:00
// UserIDClaim claim for the user id
2023-03-30 18:54:55 +02:00
UserIDClaim = "sub"
)
2023-03-30 18:59:35 +02:00
// ExtractClaims Extract function type
type ExtractClaims func(r *http.Request) AuthorizationClaims
// ClaimsExtractor struct that holds the extract function
type ClaimsExtractor struct {
authAudience string
userIDClaim string
FromRequestContext ExtractClaims
}
// ClaimsExtractorOption is a function that configures the ClaimsExtractor
type ClaimsExtractorOption func(*ClaimsExtractor)
// WithAudience sets the audience for the extractor
func WithAudience(audience string) ClaimsExtractorOption {
return func(c *ClaimsExtractor) {
c.authAudience = audience
}
}
// WithUserIDClaim sets the user id claim for the extractor
func WithUserIDClaim(userIDClaim string) ClaimsExtractorOption {
return func(c *ClaimsExtractor) {
c.userIDClaim = userIDClaim
}
}
// WithFromRequestContext sets the function that extracts claims from the request context
func WithFromRequestContext(ec ExtractClaims) ClaimsExtractorOption {
return func(c *ClaimsExtractor) {
c.FromRequestContext = ec
}
}
// NewClaimsExtractor returns an extractor, and if provided with a function with ExtractClaims signature,
// then it will use that logic. Uses ExtractClaimsFromRequestContext by default
func NewClaimsExtractor(options ...ClaimsExtractorOption) *ClaimsExtractor {
ce := &ClaimsExtractor{}
for _, option := range options {
option(ce)
}
if ce.FromRequestContext == nil {
ce.FromRequestContext = ce.fromRequestContext
}
if ce.userIDClaim == "" {
2023-03-30 18:59:35 +02:00
ce.userIDClaim = UserIDClaim
}
return ce
}
// FromToken extracts claims from the token (after auth)
func (c *ClaimsExtractor) FromToken(token *jwt.Token) AuthorizationClaims {
claims := token.Claims.(jwt.MapClaims)
jwtClaims := AuthorizationClaims{}
userID, ok := claims[c.userIDClaim].(string)
if !ok {
return jwtClaims
}
jwtClaims.UserId = userID
2023-03-30 18:54:55 +02:00
accountIDClaim, ok := claims[c.authAudience+AccountIDSuffix]
if ok {
jwtClaims.AccountId = accountIDClaim.(string)
}
2023-03-30 18:54:55 +02:00
domainClaim, ok := claims[c.authAudience+DomainIDSuffix]
if ok {
jwtClaims.Domain = domainClaim.(string)
}
2023-03-30 18:54:55 +02:00
domainCategoryClaim, ok := claims[c.authAudience+DomainCategorySuffix]
if ok {
jwtClaims.DomainCategory = domainCategoryClaim.(string)
}
return jwtClaims
}
// fromRequestContext extracts claims from the request context previously filled by the JWT token (after auth)
func (c *ClaimsExtractor) fromRequestContext(r *http.Request) AuthorizationClaims {
if r.Context().Value(TokenUserProperty) == nil {
return AuthorizationClaims{}
}
token := r.Context().Value(TokenUserProperty).(*jwt.Token)
return c.FromToken(token)
}