Skip to content

Commit 8fffb06

Browse files
authored
Add regenerate secret feature for oauth2 (#6291)
* Add regenerate secret functionality * Fix lint
1 parent 8211e01 commit 8fffb06

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

routers/routes/routes.go

+1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ func RegisterRoutes(m *macaron.Macaron) {
302302
m.Group("/applications/oauth2", func() {
303303
m.Get("/:id", userSetting.OAuth2ApplicationShow)
304304
m.Post("/:id", bindIgnErr(auth.EditOAuth2ApplicationForm{}), userSetting.OAuthApplicationsEdit)
305+
m.Post("/:id/regenerate_secret", userSetting.OAuthApplicationsRegenerateSecret)
305306
m.Post("", bindIgnErr(auth.EditOAuth2ApplicationForm{}), userSetting.OAuthApplicationsPost)
306307
m.Post("/delete", userSetting.DeleteOAuth2Application)
307308
})

routers/user/setting/oauth2.go

+28
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,34 @@ func OAuthApplicationsEdit(ctx *context.Context, form auth.EditOAuth2Application
7878
ctx.HTML(200, tplSettingsOAuthApplications)
7979
}
8080

81+
// OAuthApplicationsRegenerateSecret handles the post request for regenerating the secret
82+
func OAuthApplicationsRegenerateSecret(ctx *context.Context) {
83+
ctx.Data["Title"] = ctx.Tr("settings")
84+
ctx.Data["PageIsSettingsApplications"] = true
85+
86+
app, err := models.GetOAuth2ApplicationByID(ctx.ParamsInt64("id"))
87+
if err != nil {
88+
if models.IsErrOAuthApplicationNotFound(err) {
89+
ctx.NotFound("Application not found", err)
90+
return
91+
}
92+
ctx.ServerError("GetOAuth2ApplicationByID", err)
93+
return
94+
}
95+
if app.UID != ctx.User.ID {
96+
ctx.NotFound("Application not found", nil)
97+
return
98+
}
99+
ctx.Data["App"] = app
100+
ctx.Data["ClientSecret"], err = app.GenerateClientSecret()
101+
if err != nil {
102+
ctx.ServerError("GenerateClientSecret", err)
103+
return
104+
}
105+
ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success"))
106+
ctx.HTML(200, tplSettingsOAuthApplications)
107+
}
108+
81109
// OAuth2ApplicationShow displays the given application
82110
func OAuth2ApplicationShow(ctx *context.Context) {
83111
app, err := models.GetOAuth2ApplicationByID(ctx.ParamsInt64("id"))

templates/user/settings/applications_oauth2_edit.tmpl

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
<div class="item">
3131
<!-- TODO add regenerate secret functionality */ -->
3232
{{.i18n.Tr "settings.oauth2_regenerate_secret_hint"}}
33-
<a onclick="alert('Not yet implemented.')">{{.i18n.Tr "settings.oauth2_regenerate_secret"}}</a>
33+
<form class="ui form ignore-dirty" action="{{$.AppSubURL}}/user/settings/applications/oauth2/{{.App.ID}}/regenerate_secret" method="post">
34+
{{.CsrfTokenHtml}}
35+
<a href="#" onclick="event.target.parentNode.submit()">{{.i18n.Tr "settings.oauth2_regenerate_secret"}}</a>
36+
</form>
3437
</div>
3538
</div>
3639
<div class="ui attached bottom segment">

0 commit comments

Comments
 (0)