Skip to content

Commit 6e588f3

Browse files
authored
Merge pull request #954 from gotify/logout-swagger
fix: logout to /auth/logout and swagger docs
2 parents 838f0e7 + d432900 commit 6e588f3

File tree

5 files changed

+144
-7
lines changed

5 files changed

+144
-7
lines changed

api/session.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,34 @@ type SessionAPI struct {
2424
SecureCookie bool
2525
}
2626

27-
// Login authenticates via basic auth, creates a client, sets an HttpOnly cookie, and returns user info.
27+
// swagger:operation POST /auth/local/login auth localLogin
28+
//
29+
// Authenticate via basic auth and create a session.
30+
//
31+
// ---
32+
// consumes: [application/x-www-form-urlencoded]
33+
// produces: [application/json]
34+
// security:
35+
// - basicAuth: []
36+
// parameters:
37+
// - name: name
38+
// in: formData
39+
// description: the client name to create
40+
// required: true
41+
// type: string
42+
// responses:
43+
// 200:
44+
// description: Ok
45+
// schema:
46+
// $ref: "#/definitions/UserExternal"
47+
// headers:
48+
// Set-Cookie:
49+
// type: string
50+
// description: session cookie
51+
// 401:
52+
// description: Unauthorized
53+
// schema:
54+
// $ref: "#/definitions/Error"
2855
func (a *SessionAPI) Login(ctx *gin.Context) {
2956
name, pass, ok := ctx.Request.BasicAuth()
3057
if !ok {
@@ -65,7 +92,29 @@ func (a *SessionAPI) Login(ctx *gin.Context) {
6592
})
6693
}
6794

68-
// Logout deletes the client for the current session and clears the cookie.
95+
// swagger:operation POST /auth/logout auth logout
96+
//
97+
// End the current session.
98+
//
99+
// Clears the session cookie and deletes the associated client.
100+
//
101+
// ---
102+
// produces: [application/json]
103+
// security:
104+
// - clientTokenHeader: []
105+
// - clientTokenQuery: []
106+
// - basicAuth: []
107+
// responses:
108+
// 200:
109+
// description: Ok
110+
// headers:
111+
// Set-Cookie:
112+
// type: string
113+
// description: cleared session cookie
114+
// 400:
115+
// description: Bad Request
116+
// schema:
117+
// $ref: "#/definitions/Error"
69118
func (a *SessionAPI) Logout(ctx *gin.Context) {
70119
auth.SetCookie(ctx.Writer, "", -1, a.SecureCookie)
71120

api/session_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (s *SessionSuite) Test_Logout_Success() {
112112
builder := s.db.User(5)
113113
builder.ClientWithToken(1, "Ctesttoken12345")
114114

115-
s.ctx.Request = httptest.NewRequest("POST", "/auth/local/logout", nil)
115+
s.ctx.Request = httptest.NewRequest("POST", "/auth/logout", nil)
116116
test.WithUser(s.ctx, 5)
117117
s.ctx.Set("tokenid", "Ctesttoken12345")
118118

docs/spec.json

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,96 @@
587587
}
588588
}
589589
},
590+
"/auth/local/login": {
591+
"post": {
592+
"security": [
593+
{
594+
"basicAuth": []
595+
}
596+
],
597+
"consumes": [
598+
"application/x-www-form-urlencoded"
599+
],
600+
"produces": [
601+
"application/json"
602+
],
603+
"tags": [
604+
"auth"
605+
],
606+
"summary": "Authenticate via basic auth and create a session.",
607+
"operationId": "localLogin",
608+
"parameters": [
609+
{
610+
"type": "string",
611+
"description": "the client name to create",
612+
"name": "name",
613+
"in": "formData",
614+
"required": true
615+
}
616+
],
617+
"responses": {
618+
"200": {
619+
"description": "Ok",
620+
"schema": {
621+
"$ref": "#/definitions/UserExternal"
622+
},
623+
"headers": {
624+
"Set-Cookie": {
625+
"type": "string",
626+
"description": "session cookie"
627+
}
628+
}
629+
},
630+
"401": {
631+
"description": "Unauthorized",
632+
"schema": {
633+
"$ref": "#/definitions/Error"
634+
}
635+
}
636+
}
637+
}
638+
},
639+
"/auth/logout": {
640+
"post": {
641+
"security": [
642+
{
643+
"clientTokenHeader": []
644+
},
645+
{
646+
"clientTokenQuery": []
647+
},
648+
{
649+
"basicAuth": []
650+
}
651+
],
652+
"description": "Clears the session cookie and deletes the associated client.",
653+
"produces": [
654+
"application/json"
655+
],
656+
"tags": [
657+
"auth"
658+
],
659+
"summary": "End the current session.",
660+
"operationId": "logout",
661+
"responses": {
662+
"200": {
663+
"description": "Ok",
664+
"headers": {
665+
"Set-Cookie": {
666+
"type": "string",
667+
"description": "cleared session cookie"
668+
}
669+
}
670+
},
671+
"400": {
672+
"description": "Bad Request",
673+
"schema": {
674+
"$ref": "#/definitions/Error"
675+
}
676+
}
677+
}
678+
}
679+
},
590680
"/auth/oidc/callback": {
591681
"get": {
592682
"description": "Exchanges the authorization code for tokens, resolves the user,\ncreates a gotify client, sets a session cookie, and redirects to the UI.",

router/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func Create(db *database.GormDatabase, vInfo *model.VersionInfo, conf *config.Co
230230

231231
clientAuth.POST("current/user/password", userHandler.ChangePassword)
232232

233-
clientAuth.POST("/auth/local/logout", sessionHandler.Logout)
233+
clientAuth.POST("/auth/logout", sessionHandler.Logout)
234234
}
235235

236236
authAdmin := g.Group("/user")

ui/src/CurrentUser.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@ export class CurrentUser {
119119
runInAction(() => {
120120
this.loggedIn = false;
121121
});
122-
await axios
123-
.post(config.get('url') + 'auth/local/logout')
124-
.catch(() => Promise.resolve());
122+
await axios.post(config.get('url') + 'auth/logout').catch(() => Promise.resolve());
125123
}
126124
};
127125

0 commit comments

Comments
 (0)