@Summary Post external auth device by ID @ID post-external-auth-device-by-id @Security CoderSessionToken @Tags Git @Param externalauth path string true "External Provider ID" format(string) @Success 204 @Router /api/v2/external-auth/{externalauth}/device [post]
(rw http.ResponseWriter, r *http.Request)
| 144 | // @Success 204 |
| 145 | // @Router /api/v2/external-auth/{externalauth}/device [post] |
| 146 | func (api *API) postExternalAuthDeviceByID(rw http.ResponseWriter, r *http.Request) { |
| 147 | ctx := r.Context() |
| 148 | apiKey := httpmw.APIKey(r) |
| 149 | config := httpmw.ExternalAuthParam(r) |
| 150 | |
| 151 | var req codersdk.ExternalAuthDeviceExchange |
| 152 | if !httpapi.Read(ctx, rw, r, &req) { |
| 153 | return |
| 154 | } |
| 155 | |
| 156 | if config.DeviceAuth == nil { |
| 157 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 158 | Message: "Git auth provider does not support device flow.", |
| 159 | }) |
| 160 | return |
| 161 | } |
| 162 | |
| 163 | token, err := config.DeviceAuth.ExchangeDeviceCode(ctx, req.DeviceCode) |
| 164 | if err != nil { |
| 165 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 166 | Message: "Failed to exchange device code.", |
| 167 | Detail: err.Error(), |
| 168 | }) |
| 169 | return |
| 170 | } |
| 171 | |
| 172 | _, err = api.Database.GetExternalAuthLink(ctx, database.GetExternalAuthLinkParams{ |
| 173 | ProviderID: config.ID, |
| 174 | UserID: apiKey.UserID, |
| 175 | }) |
| 176 | if err != nil { |
| 177 | if !errors.Is(err, sql.ErrNoRows) { |
| 178 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 179 | Message: "Failed to get external auth link.", |
| 180 | Detail: err.Error(), |
| 181 | }) |
| 182 | return |
| 183 | } |
| 184 | |
| 185 | _, err = api.Database.InsertExternalAuthLink(ctx, database.InsertExternalAuthLinkParams{ |
| 186 | ProviderID: config.ID, |
| 187 | UserID: apiKey.UserID, |
| 188 | CreatedAt: dbtime.Now(), |
| 189 | UpdatedAt: dbtime.Now(), |
| 190 | OAuthAccessToken: token.AccessToken, |
| 191 | OAuthAccessTokenKeyID: sql.NullString{}, // dbcrypt will set as required |
| 192 | OAuthRefreshToken: token.RefreshToken, |
| 193 | OAuthRefreshTokenKeyID: sql.NullString{}, // dbcrypt will set as required |
| 194 | OAuthExpiry: token.Expiry, |
| 195 | // No extra data from device auth! |
| 196 | OAuthExtra: pqtype.NullRawMessage{}, |
| 197 | }) |
| 198 | if err != nil { |
| 199 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 200 | Message: "Failed to insert external auth link.", |
| 201 | Detail: err.Error(), |
| 202 | }) |
| 203 | return |
nothing calls this directly
no test coverage detected