-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_token.go
179 lines (166 loc) · 5.76 KB
/
service_token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package keycloak
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/dgrijalva/jwt-go/v4"
"github.com/google/go-querystring/query"
)
type TokenService struct {
c *APIClient
}
func (c *APIClient) TokenService() *TokenService {
ps := new(TokenService)
ps.c = c
return ps
}
// ClientEntitlement will attempt to call the pre-uma2 entitlement endpoint to return a Requesting Party Token
// containing details about what aspects of the provided clientID the token for this request has access to, if any.
// DEPRECATED: use the newer token workflow for instances newer than 3.4
func (ts *TokenService) ClientEntitlement(ctx context.Context, realmName string, ap AuthenticationProvider, clientID string, claimsType jwt.Claims, parserOpts []jwt.ParserOption, mutators ...APIRequestMutator) (*jwt.Token, error) {
var (
resp *http.Response
env *RealmEnvironment
err error
)
if env, err = ts.c.RealmEnvironment(ctx, realmName); err != nil {
return nil, err
}
// if the keycloak instance supports uma2, use that.
if env.SupportsUMA2() {
req := NewOpenIDConnectTokenRequest(GrantTypeUMA2Ticket)
req.Audience = clientID
return ts.RequestingPartyToken(ctx, realmName, ap, req, claimsType, parserOpts, mutators...)
}
// otherwise, execute legacy entitlement api
rptResp := new(struct {
RPT string `json:"rpt"`
})
resp, err = ts.c.Call(
ctx,
ap,
http.MethodGet,
ts.c.realmsURL(realmName, kcPathPartAuthz, kcPathPartEntitlement, clientID),
nil,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
)
if err = handleResponse(resp, http.StatusOK, rptResp, err); err != nil {
return nil, err
}
return ts.c.ParseToken(ctx, rptResp.RPT, claimsType, parserOpts...)
}
// PermissionEvaluation will return an array of permissions granted by the server
func (ts *TokenService) PermissionEvaluation(ctx context.Context, realmName string, ap AuthenticationProvider, req *OpenIDConnectTokenRequest, mutators ...APIRequestMutator) (EvaluatedPermissions, error) {
var (
body url.Values
resp *http.Response
env *RealmEnvironment
perms EvaluatedPermissions
err error
mode = UMA2ResponseModePermissions
)
if env, err = ts.c.RealmEnvironment(ctx, realmName); err != nil {
return nil, err
}
req.ResponseMode = &mode
if body, err = query.Values(req); err != nil {
return nil, fmt.Errorf("error encoding request: %w", err)
}
resp, err = ts.c.Call(
ctx,
ap,
http.MethodPost,
env.TokenEndpoint(),
strings.NewReader(body.Encode()),
requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
HeaderMutator(httpHeaderContentType, httpHeaderValueFormURLEncoded, true),
)...,
)
perms = make(EvaluatedPermissions, 0)
if err = handleResponse(resp, http.StatusOK, &perms, err); err != nil {
return nil, err
}
return perms, nil
}
// PermissionDecision can be used to determine whether a bearer token is allowed the permission requested
func (ts *TokenService) PermissionDecision(ctx context.Context, realmName string, ap AuthenticationProvider, req *OpenIDConnectTokenRequest, mutators ...APIRequestMutator) (*PermissionDecisionResponse, error) {
var (
res interface{}
resT *PermissionDecisionResponse
ok bool
err error
mode = UMA2ResponseModeDecision
)
req.ResponseMode = &mode
if res, err = ts.c.openIDConnectToken(ctx, realmName, ap, req, mutators...); err != nil {
if IsAPIError(err) && err.(*APIError).ResponseCode != http.StatusForbidden {
return nil, err
}
return &PermissionDecisionResponse{Result: false}, err
}
if resT, ok = res.(*PermissionDecisionResponse); !ok {
return nil, fmt.Errorf("expected response to be %T, saw %T", resT, res)
}
return resT, nil
}
func (ts *TokenService) OpenIDConnectToken(ctx context.Context, realmName string, ap AuthenticationProvider, req *OpenIDConnectTokenRequest, mutators ...APIRequestMutator) (*OpenIDConnectToken, error) {
var (
res interface{}
token *OpenIDConnectToken
ok bool
err error
)
req.ResponseMode = nil
if res, err = ts.c.openIDConnectToken(ctx, realmName, ap, req, mutators...); err != nil {
return nil, err
}
if token, ok = res.(*OpenIDConnectToken); !ok {
return nil, fmt.Errorf("expected response to be %T, saw %T", token, res)
}
return token, nil
}
// RequestingPartyToken will attempt to automatically decode and validate a RPT returned from an OIDC token request
func (ts *TokenService) RequestingPartyToken(ctx context.Context, realmName string, ap AuthenticationProvider, req *OpenIDConnectTokenRequest, claimsType jwt.Claims, parserOpts []jwt.ParserOption, mutators ...APIRequestMutator) (*jwt.Token, error) {
req.ResponseMode = nil
resp, err := ts.OpenIDConnectToken(ctx, realmName, ap, req, mutators...)
if err != nil {
return nil, err
}
return ts.c.ParseToken(ctx, resp.AccessToken, claimsType, parserOpts...)
}
func (ts *TokenService) IntrospectRequestingPartyToken(ctx context.Context, realmName string, ap AuthenticationProvider, rawRPT string, mutators ...APIRequestMutator) (*TokenIntrospectionResults, error) {
var (
body url.Values
resp *http.Response
results *TokenIntrospectionResults
env *RealmEnvironment
err error
)
if env, err = ts.c.RealmEnvironment(ctx, realmName); err != nil {
return nil, err
}
body = make(url.Values)
body.Add(paramTokenTypeHint, TokenTypeHintRequestingPartyToken)
body.Add(paramTypeToken, rawRPT)
resp, err = ts.c.Call(
ctx,
ap,
http.MethodPost,
env.IntrospectionEndpoint(),
strings.NewReader(body.Encode()),
requestMutators(
mutators,
HeaderMutator(httpHeaderAccept, httpHeaderValueJSON, true),
HeaderMutator(httpHeaderContentType, httpHeaderValueFormURLEncoded, true),
)...,
)
results = new(TokenIntrospectionResults)
if err = handleResponse(resp, http.StatusOK, results, err); err != nil {
return nil, err
}
return results, nil
}