@@ -21,46 +21,45 @@ import (
21
21
// @Router /api/v1/application/{client_id} [GET]
22
22
func GETOneApplicationDetail (c * gin.Context ) {
23
23
var err error
24
- // fetch clientID
25
- clientID := strings .TrimPrefix (c .Param ("client_id" ), "/" ) // trim due to router catch-all
26
- // get application
24
+ // fetch application info
27
25
var application models.Application
28
- if application , err = handlers .Handler .RetrieveApplication (clientID ); err != nil {
29
- c .JSON (http .StatusNotFound , datatransfers.Response {Error : "application not found" })
26
+ application .ClientID = strings .TrimPrefix (c .Param ("client_id" ), "/" ) // trim due to router catch-all
27
+ if application , err = handlers .Handler .RetrieveApplication (application .ClientID ); err != nil {
28
+ c .JSON (http .StatusNotFound , datatransfers.APIResponse {Error : "application not found" })
30
29
return
31
30
}
32
- // check ownership
33
- if application . Owner . Subject != c . GetString (constants .UserSubjectKey ) {
34
- c .JSON (http .StatusOK , datatransfers.Response {Data : datatransfers.ApplicationInfo {
31
+ // check superuser
32
+ if ! c . GetBool (constants .IsSuperuserKey ) {
33
+ c .JSON (http .StatusOK , datatransfers.APIResponse {Data : datatransfers.ApplicationInfo {
35
34
Name : application .Name ,
36
35
}})
37
36
return
38
37
}
39
- c .JSON (http .StatusOK , datatransfers.Response {Data : datatransfers.ApplicationInfo {
40
- ClientID : application .ClientID ,
41
- ClientSecret : application .ClientSecret ,
42
- Name : application .Name ,
43
- Description : application .Description ,
44
- LoginURL : application .LoginURL ,
45
- CallbackURL : application .CallbackURL ,
46
- LogoutURL : application .LogoutURL ,
47
- Metadata : application .Metadata ,
48
- CreatedAt : application .CreatedAt ,
38
+ c .JSON (http .StatusOK , datatransfers.APIResponse {Data : datatransfers.ApplicationInfo {
39
+ ClientID : application .ClientID ,
40
+ Name : application .Name ,
41
+ Description : application .Description ,
42
+ LoginURL : application .LoginURL ,
43
+ CallbackURL : application .CallbackURL ,
44
+ LogoutURL : application .LogoutURL ,
45
+ Metadata : application .Metadata ,
46
+ Locked : application .Locked ,
47
+ CreatedAt : application .CreatedAt ,
49
48
}})
50
49
return
51
50
}
52
51
53
- // @Summary Get owned applications
52
+ // @Summary Get all applications
54
53
// @Tags application
55
54
// @Security BearerAuth
56
55
// @Success 200 "OK"
57
56
// @Router /api/v1/application [GET]
58
- func GETOwnedApplications (c * gin.Context ) {
57
+ func GETApplicationList (c * gin.Context ) {
59
58
var err error
60
59
// get all owned applications
61
60
var applications []models.Application
62
- if applications , err = handlers .Handler .RetrieveOwnedApplications ( c . GetString ( constants . UserSubjectKey ) ); err != nil {
63
- c .JSON (http .StatusNotFound , datatransfers.Response {Error : "cannot retrieve applications" })
61
+ if applications , err = handlers .Handler .RetrieveAllApplications ( ); err != nil {
62
+ c .JSON (http .StatusNotFound , datatransfers.APIResponse {Error : "cannot retrieve applications" })
64
63
return
65
64
}
66
65
var applicationsResponse []datatransfers.ApplicationInfo
@@ -72,7 +71,7 @@ func GETOwnedApplications(c *gin.Context) {
72
71
CreatedAt : application .CreatedAt ,
73
72
})
74
73
}
75
- c .JSON (http .StatusOK , datatransfers.Response {Data : applicationsResponse })
74
+ c .JSON (http .StatusOK , datatransfers.APIResponse {Data : applicationsResponse })
76
75
return
77
76
}
78
77
@@ -87,15 +86,16 @@ func POSTApplication(c *gin.Context) {
87
86
// fetch application info
88
87
var applicationInfo datatransfers.ApplicationInfo
89
88
if err = c .ShouldBindJSON (& applicationInfo ); err != nil {
90
- c .JSON (http .StatusBadRequest , datatransfers.Response {Error : err .Error ()})
89
+ c .JSON (http .StatusBadRequest , datatransfers.APIResponse {Error : err .Error ()})
91
90
return
92
91
}
93
92
// register application
94
- if applicationInfo .ClientID , err = handlers .Handler .RegisterApplication (applicationInfo , c .GetString (constants .UserSubjectKey )); err != nil {
95
- c .JSON (http .StatusInternalServerError , datatransfers.Response {Error : "failed updating application" })
93
+ var clientSecret string
94
+ if applicationInfo .ClientID , clientSecret , err = handlers .Handler .RegisterApplication (applicationInfo , c .GetString (constants .UserSubjectKey )); err != nil {
95
+ c .JSON (http .StatusInternalServerError , datatransfers.APIResponse {Error : "failed updating application" })
96
96
return
97
97
}
98
- c .JSON (http .StatusOK , datatransfers.Response {Data : gin.H {"client_id" : applicationInfo .ClientID }})
98
+ c .JSON (http .StatusOK , datatransfers.APIResponse {Data : gin.H {"client_id" : applicationInfo .ClientID , "client_secret" : clientSecret }})
99
99
return
100
100
}
101
101
@@ -109,27 +109,83 @@ func POSTApplication(c *gin.Context) {
109
109
func PUTApplication (c * gin.Context ) {
110
110
var err error
111
111
// fetch application info
112
- clientID := c .Param ("client_id" )
113
112
var applicationInfo datatransfers.ApplicationInfo
114
113
if err = c .ShouldBindJSON (& applicationInfo ); err != nil {
115
- c .JSON (http .StatusBadRequest , datatransfers.Response {Error : err .Error ()})
114
+ c .JSON (http .StatusBadRequest , datatransfers.APIResponse {Error : err .Error ()})
116
115
return
117
116
}
118
- // check ownership
119
117
var application models.Application
120
- if application , err = handlers .Handler .RetrieveApplication (clientID ); err != nil {
121
- c .JSON (http .StatusNotFound , datatransfers.Response {Error : "application not found" })
118
+ application .ClientID = strings .TrimPrefix (c .Param ("client_id" ), "/" )
119
+ if application , err = handlers .Handler .RetrieveApplication (application .ClientID ); err != nil {
120
+ c .JSON (http .StatusNotFound , datatransfers.APIResponse {Error : "application not found" })
122
121
return
123
122
}
124
- if application .Owner .Subject != c .GetString (constants .UserSubjectKey ) {
125
- c .JSON (http .StatusUnauthorized , datatransfers.Response {Error : "access to resource unauthorized" })
123
+ // checked locked flag
124
+ if application .Locked &&
125
+ (applicationInfo .LoginURL != application .LoginURL ||
126
+ applicationInfo .CallbackURL != application .CallbackURL ||
127
+ applicationInfo .LogoutURL != application .LogoutURL ) {
128
+ c .JSON (http .StatusBadRequest , datatransfers.APIResponse {Error : "application is locked" })
126
129
return
127
130
}
128
131
// update application
129
132
if err = handlers .Handler .UpdateApplication (applicationInfo ); err != nil {
130
- c .JSON (http .StatusInternalServerError , datatransfers.Response {Error : "failed updating application" })
133
+ c .JSON (http .StatusInternalServerError , datatransfers.APIResponse {Error : "failed updating application" })
134
+ return
135
+ }
136
+ c .JSON (http .StatusOK , datatransfers.APIResponse {})
137
+ return
138
+ }
139
+
140
+ // @Summary Delete application
141
+ // @Tags application
142
+ // @Security BearerAuth
143
+ // @Param client_id path string true "Client ID"
144
+ // @Success 200 "OK"
145
+ // @Router /api/v1/application/{client_id} [DELETE]
146
+ func DELETEApplication (c * gin.Context ) {
147
+ var err error
148
+ // fetch application info
149
+ var application models.Application
150
+ application .ClientID = strings .TrimPrefix (c .Param ("client_id" ), "/" )
151
+ if application , err = handlers .Handler .RetrieveApplication (application .ClientID ); err != nil {
152
+ c .JSON (http .StatusNotFound , datatransfers.APIResponse {Error : "application not found" })
153
+ return
154
+ }
155
+ // checked locked flag
156
+ if application .Locked {
157
+ c .JSON (http .StatusBadRequest , datatransfers.APIResponse {Error : "application is locked" })
158
+ return
159
+ }
160
+ // delete application
161
+ if err = handlers .Handler .DeleteApplication (application .ClientID ); err != nil {
162
+ c .JSON (http .StatusInternalServerError , datatransfers.APIResponse {Error : "failed deleting application" })
163
+ return
164
+ }
165
+ c .JSON (http .StatusOK , datatransfers.APIResponse {})
166
+ return
167
+ }
168
+
169
+ // @Summary Revoke application secret
170
+ // @Tags application
171
+ // @Security BearerAuth
172
+ // @Param client_id path string true "Client ID"
173
+ // @Success 200 "OK"
174
+ // @Router /api/v1/application/{client_id}/revoke [PUT]
175
+ func PUTApplicationRevokeSecret (c * gin.Context ) {
176
+ var err error
177
+ // fetch application info
178
+ clientID := strings .TrimPrefix (c .Param ("client_id" ), "/" )
179
+ if _ , err = handlers .Handler .RetrieveApplication (clientID ); err != nil {
180
+ c .JSON (http .StatusNotFound , datatransfers.APIResponse {Error : "application not found" })
181
+ return
182
+ }
183
+ // renew application client_secret
184
+ var clientSecret string
185
+ if clientSecret , err = handlers .Handler .RenewApplicationClientSecret (clientID ); err != nil {
186
+ c .JSON (http .StatusInternalServerError , datatransfers.APIResponse {Error : "failed renewing application client_secret" })
131
187
return
132
188
}
133
- c .JSON (http .StatusOK , datatransfers.Response { })
189
+ c .JSON (http .StatusOK , datatransfers.APIResponse { Data : gin. H { "client_secret" : clientSecret } })
134
190
return
135
191
}
0 commit comments