2019-03-08 09:42:50 -07:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
2019-04-17 01:18:16 -07:00
|
|
|
"fmt"
|
2021-04-05 08:30:52 -07:00
|
|
|
"net/http"
|
2019-04-17 01:18:16 -07:00
|
|
|
|
2022-01-02 06:12:35 -07:00
|
|
|
"code.gitea.io/gitea/models/auth"
|
2019-03-08 09:42:50 -07:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-01-26 08:36:53 -07:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-04-06 12:44:05 -07:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2019-03-08 09:42:50 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tplSettingsOAuthApplications base.TplName = "user/settings/applications_oauth2_edit"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OAuthApplicationsPost response for adding a oauth2 application
|
2021-01-26 08:36:53 -07:00
|
|
|
func OAuthApplicationsPost(ctx *context.Context) {
|
2021-04-06 12:44:05 -07:00
|
|
|
form := web.GetForm(ctx).(*forms.EditOAuth2ApplicationForm)
|
2019-03-08 09:42:50 -07:00
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsApplications"] = true
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
loadApplicationsData(ctx)
|
|
|
|
|
2021-04-05 08:30:52 -07:00
|
|
|
ctx.HTML(http.StatusOK, tplSettingsApplications)
|
2019-03-08 09:42:50 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO validate redirect URI
|
2022-05-20 07:08:52 -07:00
|
|
|
app, err := auth.CreateOAuth2Application(ctx, auth.CreateOAuth2ApplicationOptions{
|
2019-03-08 09:42:50 -07:00
|
|
|
Name: form.Name,
|
|
|
|
RedirectURIs: []string{form.RedirectURI},
|
2022-03-22 00:03:22 -07:00
|
|
|
UserID: ctx.Doer.ID,
|
2019-03-08 09:42:50 -07:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("CreateOAuth2Application", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.create_oauth2_application_success"))
|
|
|
|
ctx.Data["App"] = app
|
|
|
|
ctx.Data["ClientSecret"], err = app.GenerateClientSecret()
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GenerateClientSecret", err)
|
|
|
|
return
|
|
|
|
}
|
2021-04-05 08:30:52 -07:00
|
|
|
ctx.HTML(http.StatusOK, tplSettingsOAuthApplications)
|
2019-03-08 09:42:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// OAuthApplicationsEdit response for editing oauth2 application
|
2021-01-26 08:36:53 -07:00
|
|
|
func OAuthApplicationsEdit(ctx *context.Context) {
|
2021-04-06 12:44:05 -07:00
|
|
|
form := web.GetForm(ctx).(*forms.EditOAuth2ApplicationForm)
|
2019-03-08 09:42:50 -07:00
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsApplications"] = true
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
loadApplicationsData(ctx)
|
|
|
|
|
2021-04-05 08:30:52 -07:00
|
|
|
ctx.HTML(http.StatusOK, tplSettingsApplications)
|
2019-03-08 09:42:50 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO validate redirect URI
|
2020-04-30 10:50:47 -07:00
|
|
|
var err error
|
2022-01-02 06:12:35 -07:00
|
|
|
if ctx.Data["App"], err = auth.UpdateOAuth2Application(auth.UpdateOAuth2ApplicationOptions{
|
2019-03-08 09:42:50 -07:00
|
|
|
ID: ctx.ParamsInt64("id"),
|
|
|
|
Name: form.Name,
|
|
|
|
RedirectURIs: []string{form.RedirectURI},
|
2022-03-22 00:03:22 -07:00
|
|
|
UserID: ctx.Doer.ID,
|
2019-03-08 09:42:50 -07:00
|
|
|
}); err != nil {
|
|
|
|
ctx.ServerError("UpdateOAuth2Application", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success"))
|
2021-04-05 08:30:52 -07:00
|
|
|
ctx.HTML(http.StatusOK, tplSettingsOAuthApplications)
|
2019-03-08 09:42:50 -07:00
|
|
|
}
|
|
|
|
|
2019-03-09 09:29:58 -07:00
|
|
|
// OAuthApplicationsRegenerateSecret handles the post request for regenerating the secret
|
|
|
|
func OAuthApplicationsRegenerateSecret(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsApplications"] = true
|
|
|
|
|
2022-05-20 07:08:52 -07:00
|
|
|
app, err := auth.GetOAuth2ApplicationByID(ctx, ctx.ParamsInt64("id"))
|
2019-03-09 09:29:58 -07:00
|
|
|
if err != nil {
|
2022-01-02 06:12:35 -07:00
|
|
|
if auth.IsErrOAuthApplicationNotFound(err) {
|
2019-03-09 09:29:58 -07:00
|
|
|
ctx.NotFound("Application not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.ServerError("GetOAuth2ApplicationByID", err)
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 00:03:22 -07:00
|
|
|
if app.UID != ctx.Doer.ID {
|
2019-03-09 09:29:58 -07:00
|
|
|
ctx.NotFound("Application not found", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["App"] = app
|
|
|
|
ctx.Data["ClientSecret"], err = app.GenerateClientSecret()
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GenerateClientSecret", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success"))
|
2021-04-05 08:30:52 -07:00
|
|
|
ctx.HTML(http.StatusOK, tplSettingsOAuthApplications)
|
2019-03-09 09:29:58 -07:00
|
|
|
}
|
|
|
|
|
2019-03-08 09:42:50 -07:00
|
|
|
// OAuth2ApplicationShow displays the given application
|
|
|
|
func OAuth2ApplicationShow(ctx *context.Context) {
|
2022-05-20 07:08:52 -07:00
|
|
|
app, err := auth.GetOAuth2ApplicationByID(ctx, ctx.ParamsInt64("id"))
|
2019-03-08 09:42:50 -07:00
|
|
|
if err != nil {
|
2022-01-02 06:12:35 -07:00
|
|
|
if auth.IsErrOAuthApplicationNotFound(err) {
|
2019-03-08 09:42:50 -07:00
|
|
|
ctx.NotFound("Application not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.ServerError("GetOAuth2ApplicationByID", err)
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 00:03:22 -07:00
|
|
|
if app.UID != ctx.Doer.ID {
|
2019-03-08 09:42:50 -07:00
|
|
|
ctx.NotFound("Application not found", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["App"] = app
|
2021-04-05 08:30:52 -07:00
|
|
|
ctx.HTML(http.StatusOK, tplSettingsOAuthApplications)
|
2019-03-08 09:42:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteOAuth2Application deletes the given oauth2 application
|
|
|
|
func DeleteOAuth2Application(ctx *context.Context) {
|
2022-03-22 00:03:22 -07:00
|
|
|
if err := auth.DeleteOAuth2Application(ctx.FormInt64("id"), ctx.Doer.ID); err != nil {
|
2019-03-08 09:42:50 -07:00
|
|
|
ctx.ServerError("DeleteOAuth2Application", err)
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 00:03:22 -07:00
|
|
|
log.Trace("OAuth2 Application deleted: %s", ctx.Doer.Name)
|
2019-03-08 09:42:50 -07:00
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.remove_oauth2_application_success"))
|
2021-04-05 08:30:52 -07:00
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2019-03-08 09:42:50 -07:00
|
|
|
"redirect": setting.AppSubURL + "/user/settings/applications",
|
|
|
|
})
|
|
|
|
}
|
2019-04-17 01:18:16 -07:00
|
|
|
|
|
|
|
// RevokeOAuth2Grant revokes the grant with the given id
|
|
|
|
func RevokeOAuth2Grant(ctx *context.Context) {
|
2022-03-22 00:03:22 -07:00
|
|
|
if ctx.Doer.ID == 0 || ctx.FormInt64("id") == 0 {
|
2019-04-17 01:18:16 -07:00
|
|
|
ctx.ServerError("RevokeOAuth2Grant", fmt.Errorf("user id or grant id is zero"))
|
|
|
|
return
|
|
|
|
}
|
2022-05-20 07:08:52 -07:00
|
|
|
if err := auth.RevokeOAuth2Grant(ctx, ctx.FormInt64("id"), ctx.Doer.ID); err != nil {
|
2019-04-17 01:18:16 -07:00
|
|
|
ctx.ServerError("RevokeOAuth2Grant", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.revoke_oauth2_grant_success"))
|
2021-04-05 08:30:52 -07:00
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2019-04-17 01:18:16 -07:00
|
|
|
"redirect": setting.AppSubURL + "/user/settings/applications",
|
|
|
|
})
|
|
|
|
}
|