github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/idtokenclaims/claims.go (about) 1 package idtokenclaims 2 3 import ( 4 "context" 5 "encoding/json" 6 "strings" 7 8 "github.com/kyma-incubator/compass/components/hydrator/pkg/tenantmapping" 9 10 "github.com/kyma-incubator/compass/components/hydrator/pkg/oathkeeper" 11 12 "github.com/pkg/errors" 13 14 "github.com/form3tech-oss/jwt-go" 15 "github.com/kyma-incubator/compass/components/director/internal/domain/tenant" 16 "github.com/kyma-incubator/compass/components/director/pkg/consumer" 17 "github.com/kyma-incubator/compass/components/director/pkg/log" 18 "github.com/kyma-incubator/compass/components/director/pkg/scope" 19 ) 20 21 // Claims missing godoc 22 type Claims struct { 23 Tenant map[string]string `json:"tenant"` 24 Scopes string `json:"scopes"` 25 ConsumerID string `json:"consumerID"` 26 ConsumerType consumer.ConsumerType `json:"consumerType"` 27 OnBehalfOf string `json:"onBehalfOf"` 28 Region string `json:"region"` 29 TokenClientID string `json:"tokenClientID"` 30 Flow oathkeeper.AuthFlow `json:"flow"` 31 ZID string `json:"zid"` 32 jwt.StandardClaims 33 } 34 35 // UnmarshalJSON implements Unmarshaler interface. The method unmarshal the data from b into Claims structure. 36 func (c *Claims) UnmarshalJSON(b []byte) error { 37 tokenClaims := struct { 38 TenantString string `json:"tenant"` 39 Scopes string `json:"scopes"` 40 ConsumerID string `json:"consumerID"` 41 ConsumerType consumer.ConsumerType `json:"consumerType"` 42 OnBehalfOf string `json:"onBehalfOf"` 43 Region string `json:"region"` 44 TokenClientID string `json:"tokenClientID"` 45 Flow oathkeeper.AuthFlow `json:"flow"` 46 ZID string `json:"zid"` 47 jwt.StandardClaims 48 }{} 49 50 err := json.Unmarshal(b, &tokenClaims) 51 if err != nil { 52 return errors.Wrap(err, "while unmarshaling token claims:") 53 } 54 55 c.Scopes = tokenClaims.Scopes 56 c.ConsumerID = tokenClaims.ConsumerID 57 c.ConsumerType = tokenClaims.ConsumerType 58 c.OnBehalfOf = tokenClaims.OnBehalfOf 59 c.Region = tokenClaims.Region 60 c.TokenClientID = tokenClaims.TokenClientID 61 c.Flow = tokenClaims.Flow 62 c.ZID = tokenClaims.ZID 63 c.StandardClaims = tokenClaims.StandardClaims 64 65 if err := json.Unmarshal([]byte(tokenClaims.TenantString), &c.Tenant); err != nil { 66 log.D().Warnf("While unmarshaling tenants: %+v", err) 67 c.Tenant = make(map[string]string) 68 } 69 70 return nil 71 } 72 73 // ContextWithClaims missing godoc 74 func (c *Claims) ContextWithClaims(ctx context.Context) context.Context { 75 ctxWithTenants := tenant.SaveToContext(ctx, c.Tenant[tenantmapping.ConsumerTenantKey], c.Tenant[tenantmapping.ExternalTenantKey]) 76 scopesArray := strings.Split(c.Scopes, " ") 77 ctxWithScopes := scope.SaveToContext(ctxWithTenants, scopesArray) 78 apiConsumer := consumer.Consumer{ConsumerID: c.ConsumerID, ConsumerType: c.ConsumerType, Flow: c.Flow, OnBehalfOf: c.OnBehalfOf, Region: c.Region, TokenClientID: c.TokenClientID} 79 ctxWithConsumerInfo := consumer.SaveToContext(ctxWithScopes, apiConsumer) 80 return ctxWithConsumerInfo 81 } 82 83 // ContextWithClaimsAndProviderTenant stores token data in context. Stores the provider tenant into the context 84 func (c *Claims) ContextWithClaimsAndProviderTenant(ctx context.Context) context.Context { 85 ctxWithTenants := tenant.SaveToContext(ctx, c.Tenant[tenantmapping.ProviderTenantKey], c.Tenant[tenantmapping.ProviderExternalTenantKey]) 86 scopesArray := strings.Split(c.Scopes, " ") 87 ctxWithScopes := scope.SaveToContext(ctxWithTenants, scopesArray) 88 apiConsumer := consumer.Consumer{ConsumerID: c.ConsumerID, ConsumerType: c.ConsumerType, Flow: c.Flow, OnBehalfOf: c.OnBehalfOf, Region: c.Region, TokenClientID: c.TokenClientID} 89 ctxWithConsumerInfo := consumer.SaveToContext(ctxWithScopes, apiConsumer) 90 return ctxWithConsumerInfo 91 }