|
| 1 | +package oauth2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + "golang.org/x/oauth2/internal" |
| 14 | +) |
| 15 | + |
| 16 | +// https://datatracker.ietf.org/doc/html/rfc8628#section-3.5 |
| 17 | +const ( |
| 18 | + errAuthorizationPending = "authorization_pending" |
| 19 | + errSlowDown = "slow_down" |
| 20 | + errAccessDenied = "access_denied" |
| 21 | + errExpiredToken = "expired_token" |
| 22 | +) |
| 23 | + |
| 24 | +// DeviceAuthResponse https://datatracker.ietf.org/doc/html/rfc8628#section-3.2 |
| 25 | +type DeviceAuthResponse struct { |
| 26 | + DeviceCode string `json:"device_code"` |
| 27 | + UserCode string `json:"user_code"` |
| 28 | + VerificationURI string `json:"verification_uri"` |
| 29 | + VerificationURIComplete string `json:"verification_uri_complete,omitempty"` |
| 30 | + Expiry time.Time `json:"expires_in,omitempty"` |
| 31 | + Interval int64 `json:"interval,omitempty"` |
| 32 | +} |
| 33 | + |
| 34 | +func (d DeviceAuthResponse) MarshalJSON() ([]byte, error) { |
| 35 | + type Alias DeviceAuthResponse |
| 36 | + var expiresIn int64 |
| 37 | + if !d.Expiry.IsZero() { |
| 38 | + expiresIn = int64(time.Until(d.Expiry).Seconds()) |
| 39 | + } |
| 40 | + return json.Marshal(&struct { |
| 41 | + ExpiresIn int64 `json:"expires_in,omitempty"` |
| 42 | + *Alias |
| 43 | + }{ |
| 44 | + ExpiresIn: expiresIn, |
| 45 | + Alias: (*Alias)(&d), |
| 46 | + }) |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +func (c *DeviceAuthResponse) UnmarshalJSON(data []byte) error { |
| 51 | + type Alias DeviceAuthResponse |
| 52 | + aux := &struct { |
| 53 | + ExpiresIn int64 `json:"expires_in"` |
| 54 | + *Alias |
| 55 | + }{ |
| 56 | + Alias: (*Alias)(c), |
| 57 | + } |
| 58 | + if err := json.Unmarshal(data, &aux); err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + if aux.ExpiresIn != 0 { |
| 62 | + c.Expiry = time.Now().UTC().Add(time.Second * time.Duration(aux.ExpiresIn)) |
| 63 | + } |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +// DeviceAuth returns a device auth struct which contains a device code |
| 68 | +// and authorization information provided for users to enter on another device. |
| 69 | +func (c *Config) DeviceAuth(ctx context.Context, opts ...AuthCodeOption) (*DeviceAuthResponse, error) { |
| 70 | + // https://datatracker.ietf.org/doc/html/rfc8628#section-3.1 |
| 71 | + v := url.Values{ |
| 72 | + "client_id": {c.ClientID}, |
| 73 | + } |
| 74 | + if len(c.Scopes) > 0 { |
| 75 | + v.Set("scope", strings.Join(c.Scopes, " ")) |
| 76 | + } |
| 77 | + for _, opt := range opts { |
| 78 | + opt.setValue(v) |
| 79 | + } |
| 80 | + return retrieveDeviceAuth(ctx, c, v) |
| 81 | +} |
| 82 | + |
| 83 | +func retrieveDeviceAuth(ctx context.Context, c *Config, v url.Values) (*DeviceAuthResponse, error) { |
| 84 | + req, err := http.NewRequest("POST", c.Endpoint.DeviceAuthURL, strings.NewReader(v.Encode())) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 89 | + req.Header.Set("Accept", "application/json") |
| 90 | + |
| 91 | + r, err := internal.ContextClient(ctx).Do(req) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + |
| 96 | + body, err := io.ReadAll(io.LimitReader(r.Body, 1<<20)) |
| 97 | + if err != nil { |
| 98 | + return nil, fmt.Errorf("oauth2: cannot auth device: %v", err) |
| 99 | + } |
| 100 | + if code := r.StatusCode; code < 200 || code > 299 { |
| 101 | + return nil, &RetrieveError{ |
| 102 | + Response: r, |
| 103 | + Body: body, |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + da := &DeviceAuthResponse{} |
| 108 | + err = json.Unmarshal(body, &da) |
| 109 | + if err != nil { |
| 110 | + return nil, fmt.Errorf("unmarshal %s", err) |
| 111 | + } |
| 112 | + return da, nil |
| 113 | +} |
| 114 | + |
| 115 | +// Poll tries to exchange an device code for a token. |
| 116 | +func (c *Config) Poll(ctx context.Context, da *DeviceAuthResponse, opts ...AuthCodeOption) (*Token, error) { |
| 117 | + // https://datatracker.ietf.org/doc/html/rfc8628#section-3.4 |
| 118 | + v := url.Values{ |
| 119 | + "client_id": {c.ClientID}, |
| 120 | + "grant_type": {"urn:ietf:params:oauth:grant-type:device_code"}, |
| 121 | + "device_code": {da.DeviceCode}, |
| 122 | + } |
| 123 | + if len(c.Scopes) > 0 { |
| 124 | + v.Set("scope", strings.Join(c.Scopes, " ")) |
| 125 | + } |
| 126 | + for _, opt := range opts { |
| 127 | + opt.setValue(v) |
| 128 | + } |
| 129 | + |
| 130 | + // If no interval was provided, the client MUST use a reasonable default polling interval. |
| 131 | + // See https://tools.ietf.org/html/draft-ietf-oauth-device-flow-07#section-3.5 |
| 132 | + interval := da.Interval |
| 133 | + if interval == 0 { |
| 134 | + interval = 5 |
| 135 | + } |
| 136 | + |
| 137 | + for { |
| 138 | + time.Sleep(time.Duration(interval) * time.Second) |
| 139 | + |
| 140 | + tok, err := retrieveToken(ctx, c, v) |
| 141 | + if err == nil { |
| 142 | + return tok, nil |
| 143 | + } |
| 144 | + |
| 145 | + e, ok := err.(*RetrieveError) |
| 146 | + if !ok { |
| 147 | + return nil, err |
| 148 | + } |
| 149 | + switch e.ErrorCode { |
| 150 | + case errSlowDown: |
| 151 | + // https://datatracker.ietf.org/doc/html/rfc8628#section-3.5 |
| 152 | + // "the interval MUST be increased by 5 seconds for this and all subsequent requests" |
| 153 | + interval += 5 |
| 154 | + case errAuthorizationPending: |
| 155 | + // Do nothing. |
| 156 | + case errAccessDenied, errExpiredToken: |
| 157 | + fallthrough |
| 158 | + default: |
| 159 | + return tok, err |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments