2021-09-27 11:43:49 +00:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/oidc/v2/pkg/oidc"
|
2021-09-27 11:43:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Context map[string]interface{}
|
|
|
|
|
|
|
|
func (c Context) set(name string, value interface{}) {
|
|
|
|
map[string]interface{}(c)[name] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) SetToken(t *oidc.Tokens) *Context {
|
2021-10-05 06:33:20 +00:00
|
|
|
if t == nil {
|
|
|
|
return c
|
|
|
|
}
|
2021-09-27 11:43:49 +00:00
|
|
|
if t.Token != nil && t.Token.AccessToken != "" {
|
|
|
|
c.set("accessToken", t.AccessToken)
|
|
|
|
}
|
|
|
|
if t.IDToken != "" {
|
|
|
|
c.set("idToken", t.IDToken)
|
|
|
|
}
|
|
|
|
if t.IDTokenClaims != nil {
|
|
|
|
c.set("getClaim", func(claim string) interface{} { return t.IDTokenClaims.GetClaim(claim) })
|
|
|
|
c.set("claimsJSON", func() (string, error) {
|
|
|
|
c, err := json.Marshal(t.IDTokenClaims)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(c), nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|