Debian Codegraph @

pkg/client/rp/device.go

raw
1 package rp
2
3 import (
4 "context"
5 "fmt"
6 "slices"
7 "time"
8
9 "github.com/zitadel/oidc/v3/pkg/client"
10 httphelper "github.com/zitadel/oidc/v3/pkg/http"
11 "github.com/zitadel/oidc/v3/pkg/oidc"
12 "golang.org/x/oauth2"
13 )
14
15 func newDeviceClientCredentialsRequest(scopes []string, rp RelyingParty) (*oidc.ClientCredentialsRequest, error) {
16 config := rp.OAuthConfig()
17 req := &oidc.ClientCredentialsRequest{
18 Scope: scopes,
19 ClientID: config.ClientID,
20 ClientSecret: config.ClientSecret,
21 }
22
23 if signer := rp.Signer(); signer != nil {
24 assertion, err := client.SignedJWTProfileAssertion(rp.OAuthConfig().ClientID, []string{rp.Issuer()}, time.Hour, signer)
25 if err != nil {
26 return nil, fmt.Errorf("failed to build assertion: %w", err)
27 }
28 req.ClientAssertion = assertion
29 req.ClientAssertionType = oidc.ClientAssertionTypeJWTAssertion
30 }
31
32 return req, nil
33 }
34
35 // DeviceAuthorization starts a new Device Authorization flow as defined
36 // in RFC 8628, section 3.1 and 3.2:
37 // https://www.rfc-editor.org/rfc/rfc8628#section-3.1
38 // When the RelyingParty is configured with [WithKeyBinding], the `bound_key`
39 // scope and the `dpop_jkt` parameter are added.
40 func DeviceAuthorization(ctx context.Context, scopes []string, rp RelyingParty, authFn any) (*oidc.DeviceAuthorizationResponse, error) {
41 ctx, span := client.Tracer.Start(ctx, "DeviceAuthorization")
42 defer span.End()
43
44 configured, bound := keyBindingRP(rp)
45 if bound && !slices.Contains(scopes, oidc.ScopeBoundKey) {
46 scopes = append(slices.Clone(scopes), oidc.ScopeBoundKey)
47 }
48
49 req, err := newDeviceClientCredentialsRequest(scopes, rp)
50 if err != nil {
51 return nil, err
52 }
53 if !bound {
54 return client.CallDeviceAuthorizationEndpoint(ctx, req, rp, authFn)
55 }
56 return client.CallDeviceAuthorizationEndpoint(ctx, req, rp, authFn,
57 client.WithDPoPJKT(configured.KeyBindingThumbprint()))
58 }
59
60 // DeviceAccessToken attempts to obtain tokens from a Device Authorization,
61 // by means of polling as defined in RFC, section 3.3 and 3.4:
62 // https://www.rfc-editor.org/rfc/rfc8628#section-3.4
63 //
64 // When the RelyingParty is configured with [WithKeyBinding], each poll carries a
65 // DPoP proof bound to deviceCode.
66 func DeviceAccessToken(ctx context.Context, deviceCode string, interval time.Duration, rp RelyingParty) (resp *oidc.AccessTokenResponse, err error) {
67 ctx, span := client.Tracer.Start(ctx, "DeviceAccessToken")
68 defer span.End()
69
70 req := &client.DeviceAccessTokenRequest{
71 DeviceAccessTokenRequest: oidc.DeviceAccessTokenRequest{
72 GrantType: oidc.GrantTypeDeviceCode,
73 DeviceCode: deviceCode,
74 },
75 ClientCredentialsRequest: &oidc.ClientCredentialsRequest{},
76 }
77
78 var authFn httphelper.RequestAuthorization
79
80 // https://datatracker.ietf.org/doc/html/rfc6749#section-2.3
81 // The client MUST NOT use more than one authentication method in each request.
82 switch rp.OAuthConfig().Endpoint.AuthStyle {
83 case oauth2.AuthStyleInHeader:
84 authFn = httphelper.AuthorizeBasic(rp.OAuthConfig().ClientID, rp.OAuthConfig().ClientSecret)
85 default:
86 if signer := rp.Signer(); signer != nil {
87 assertion, err := client.SignedJWTProfileAssertion(rp.OAuthConfig().ClientID, []string{rp.Issuer()}, time.Hour, signer)
88 if err != nil {
89 return nil, fmt.Errorf("failed to build assertion: %w", err)
90 }
91 req.ClientAssertion = assertion
92 req.ClientAssertionType = oidc.ClientAssertionTypeJWTAssertion
93 } else {
94 req.ClientID = rp.OAuthConfig().ClientID
95 req.ClientSecret = rp.OAuthConfig().ClientSecret
96 }
97
98 }
99
100 caller := tokenEndpointCaller{RelyingParty: rp}
101 configured, bound := keyBindingRP(rp)
102 if bound {
103 // The proof is over the device code (c_s256)
104 caller.httpClient, err = keyBindingHTTPClient(rp.HttpClient(), configured, deviceCode, rp.OAuthConfig().Endpoint.TokenURL)
105 if err != nil {
106 return nil, err
107 }
108 }
109
110 resp, err = client.PollDeviceAccessTokenEndpointWithAuthFn(ctx, interval, req, caller, authFn)
111 if err != nil {
112 return nil, err
113 }
114 if bound {
115 if err := verifyDeviceKeyBinding(ctx, resp, rp, configured); err != nil {
116 return nil, err
117 }
118 }
119 return resp, nil
120 }
121
122 // verifyDeviceKeyBinding checks that the ID Token returned by the device token
123 // endpoint is actually bound to the expected RP's key.
124 func verifyDeviceKeyBinding(ctx context.Context, resp *oidc.AccessTokenResponse, rp RelyingParty, configured KeyBindingRelyingParty) error {
125 if resp.IDToken == "" {
126 return fmt.Errorf("%w: no id_token returned for a bound_key request", ErrKeyBindingIDToken)
127 }
128 idToken, err := VerifyIDToken[*oidc.IDTokenClaims](ctx, resp.IDToken, rp.IDTokenVerifier())
129 if err != nil {
130 return err
131 }
132 return verifyKeyBindingIDToken(resp.IDToken, idToken.GetSignatureAlgorithm(), configured.KeyBindingThumbprint())
133 }