Logout is the endpoint for logging out a user and thereby revoking any refresh tokens
(w http.ResponseWriter, r *http.Request)
| 19 | |
| 20 | // Logout is the endpoint for logging out a user and thereby revoking any refresh tokens |
| 21 | func (a *API) Logout(w http.ResponseWriter, r *http.Request) error { |
| 22 | ctx := r.Context() |
| 23 | config := a.config |
| 24 | db := a.db.WithContext(ctx) |
| 25 | scope := LogoutGlobal |
| 26 | |
| 27 | if r.URL.Query() != nil { |
| 28 | switch r.URL.Query().Get("scope") { |
| 29 | case "", "global": |
| 30 | scope = LogoutGlobal |
| 31 | |
| 32 | case "local": |
| 33 | scope = LogoutLocal |
| 34 | |
| 35 | case "others": |
| 36 | scope = LogoutOthers |
| 37 | |
| 38 | default: |
| 39 | return apierrors.NewBadRequestError(apierrors.ErrorCodeValidationFailed, "Unsupported logout scope %q", r.URL.Query().Get("scope")) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | s := getSession(ctx) |
| 44 | u := getUser(ctx) |
| 45 | |
| 46 | err := db.Transaction(func(tx *storage.Connection) error { |
| 47 | if terr := models.NewAuditLogEntry(config.AuditLog, r, tx, u, models.LogoutAction, "", nil); terr != nil { |
| 48 | return terr |
| 49 | } |
| 50 | |
| 51 | if s == nil { |
| 52 | logrus.Infof("user has an empty session_id claim: %s", u.ID) |
| 53 | } else { |
| 54 | switch scope { |
| 55 | case LogoutLocal: |
| 56 | return models.LogoutSession(tx, s.ID) |
| 57 | |
| 58 | case LogoutOthers: |
| 59 | return models.LogoutAllExceptMe(tx, s.ID, u.ID) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // default mode, log out everywhere |
| 64 | return models.Logout(tx, u.ID) |
| 65 | }) |
| 66 | if err != nil { |
| 67 | return apierrors.NewInternalServerError("Error logging out user").WithInternalError(err) |
| 68 | } |
| 69 | |
| 70 | w.WriteHeader(http.StatusNoContent) |
| 71 | |
| 72 | return nil |
| 73 | } |
nothing calls this directly
no test coverage detected