Verify exchanges a confirmation or recovery token to a refresh token
(w http.ResponseWriter, r *http.Request)
| 23 | |
| 24 | // Verify exchanges a confirmation or recovery token to a refresh token |
| 25 | func (a *API) Verify(w http.ResponseWriter, r *http.Request) error { |
| 26 | ctx := r.Context() |
| 27 | config := a.getConfig(ctx) |
| 28 | |
| 29 | params := &VerifyParams{} |
| 30 | cookie := r.Header.Get(useCookieHeader) |
| 31 | jsonDecoder := json.NewDecoder(r.Body) |
| 32 | if err := jsonDecoder.Decode(params); err != nil { |
| 33 | return badRequestError("Could not read verification params: %v", err) |
| 34 | } |
| 35 | |
| 36 | if params.Token == "" { |
| 37 | return unprocessableEntityError("Verify requires a token") |
| 38 | } |
| 39 | |
| 40 | var ( |
| 41 | user *models.User |
| 42 | err error |
| 43 | token *AccessTokenResponse |
| 44 | ) |
| 45 | |
| 46 | err = a.db.Transaction(func(tx *storage.Connection) error { |
| 47 | var terr error |
| 48 | switch params.Type { |
| 49 | case signupVerification: |
| 50 | user, terr = a.signupVerify(ctx, tx, params) |
| 51 | case recoveryVerification: |
| 52 | user, terr = a.recoverVerify(ctx, tx, params) |
| 53 | default: |
| 54 | return unprocessableEntityError("Verify requires a verification type") |
| 55 | } |
| 56 | |
| 57 | if terr != nil { |
| 58 | return terr |
| 59 | } |
| 60 | |
| 61 | token, terr = a.issueRefreshToken(ctx, tx, user) |
| 62 | if terr != nil { |
| 63 | return terr |
| 64 | } |
| 65 | |
| 66 | if cookie != "" && config.Cookie.Duration > 0 { |
| 67 | if terr = a.setCookieToken(config, token.Token, cookie == useSessionCookie, w); terr != nil { |
| 68 | return internalServerError("Failed to set JWT cookie. %s", terr) |
| 69 | } |
| 70 | } |
| 71 | return nil |
| 72 | }) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | |
| 77 | return sendJSON(w, http.StatusOK, token) |
| 78 | } |
| 79 | |
| 80 | func (a *API) signupVerify(ctx context.Context, conn *storage.Connection, params *VerifyParams) (*models.User, error) { |
| 81 | instanceID := getInstanceID(ctx) |
nothing calls this directly
no test coverage detected