2022-10-09 12:07:41 +00:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-10-09 12:07:41 +00:00
|
|
|
|
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/auth"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2023-06-05 07:00:12 +00:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2022-10-09 12:07:41 +00:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
|
|
|
"code.gitea.io/gitea/services/forms"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OAuth2CommonHandlers struct {
|
|
|
|
OwnerID int64 // 0 for instance-wide, otherwise OrgID or UserID
|
|
|
|
BasePathList string // the base URL for the application list page, eg: "/user/setting/applications"
|
|
|
|
BasePathEditPrefix string // the base URL for the application edit page, will be appended with app id, eg: "/user/setting/applications/oauth2"
|
|
|
|
TplAppEdit base.TplName // the template for the application edit page
|
|
|
|
}
|
|
|
|
|
|
|
|
func (oa *OAuth2CommonHandlers) renderEditPage(ctx *context.Context) {
|
|
|
|
app := ctx.Data["App"].(*auth.OAuth2Application)
|
|
|
|
ctx.Data["FormActionPath"] = fmt.Sprintf("%s/%d", oa.BasePathEditPrefix, app.ID)
|
|
|
|
ctx.HTML(http.StatusOK, oa.TplAppEdit)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddApp adds an oauth2 application
|
|
|
|
func (oa *OAuth2CommonHandlers) AddApp(ctx *context.Context) {
|
|
|
|
form := web.GetForm(ctx).(*forms.EditOAuth2ApplicationForm)
|
|
|
|
if ctx.HasError() {
|
2023-05-26 09:42:54 +00:00
|
|
|
ctx.Flash.Error(ctx.GetErrMsg())
|
2022-10-09 12:07:41 +00:00
|
|
|
// go to the application list page
|
|
|
|
ctx.Redirect(oa.BasePathList)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO validate redirect URI
|
|
|
|
app, err := auth.CreateOAuth2Application(ctx, auth.CreateOAuth2ApplicationOptions{
|
2022-10-24 07:59:24 +00:00
|
|
|
Name: form.Name,
|
2023-06-05 07:00:12 +00:00
|
|
|
RedirectURIs: util.SplitTrimSpace(form.RedirectURIs, "\n"),
|
2022-10-24 07:59:24 +00:00
|
|
|
UserID: oa.OwnerID,
|
|
|
|
ConfidentialClient: form.ConfidentialClient,
|
2022-10-09 12:07:41 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("CreateOAuth2Application", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// render the edit page with secret
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.create_oauth2_application_success"), true)
|
|
|
|
ctx.Data["App"] = app
|
|
|
|
ctx.Data["ClientSecret"], err = app.GenerateClientSecret()
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GenerateClientSecret", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
oa.renderEditPage(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditShow displays the given application
|
|
|
|
func (oa *OAuth2CommonHandlers) EditShow(ctx *context.Context) {
|
|
|
|
app, err := auth.GetOAuth2ApplicationByID(ctx, ctx.ParamsInt64("id"))
|
|
|
|
if err != nil {
|
|
|
|
if auth.IsErrOAuthApplicationNotFound(err) {
|
|
|
|
ctx.NotFound("Application not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.ServerError("GetOAuth2ApplicationByID", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if app.UID != oa.OwnerID {
|
|
|
|
ctx.NotFound("Application not found", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["App"] = app
|
|
|
|
oa.renderEditPage(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditSave saves the oauth2 application
|
|
|
|
func (oa *OAuth2CommonHandlers) EditSave(ctx *context.Context) {
|
|
|
|
form := web.GetForm(ctx).(*forms.EditOAuth2ApplicationForm)
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
oa.renderEditPage(ctx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO validate redirect URI
|
|
|
|
var err error
|
|
|
|
if ctx.Data["App"], err = auth.UpdateOAuth2Application(auth.UpdateOAuth2ApplicationOptions{
|
2022-10-24 07:59:24 +00:00
|
|
|
ID: ctx.ParamsInt64("id"),
|
|
|
|
Name: form.Name,
|
2023-06-05 07:00:12 +00:00
|
|
|
RedirectURIs: util.SplitTrimSpace(form.RedirectURIs, "\n"),
|
2022-10-24 07:59:24 +00:00
|
|
|
UserID: oa.OwnerID,
|
|
|
|
ConfidentialClient: form.ConfidentialClient,
|
2022-10-09 12:07:41 +00:00
|
|
|
}); err != nil {
|
|
|
|
ctx.ServerError("UpdateOAuth2Application", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success"))
|
|
|
|
ctx.Redirect(oa.BasePathList)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegenerateSecret regenerates the secret
|
|
|
|
func (oa *OAuth2CommonHandlers) RegenerateSecret(ctx *context.Context) {
|
|
|
|
app, err := auth.GetOAuth2ApplicationByID(ctx, ctx.ParamsInt64("id"))
|
|
|
|
if err != nil {
|
|
|
|
if auth.IsErrOAuthApplicationNotFound(err) {
|
|
|
|
ctx.NotFound("Application not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.ServerError("GetOAuth2ApplicationByID", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if app.UID != oa.OwnerID {
|
|
|
|
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"), true)
|
|
|
|
oa.renderEditPage(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteApp deletes the given oauth2 application
|
|
|
|
func (oa *OAuth2CommonHandlers) DeleteApp(ctx *context.Context) {
|
|
|
|
if err := auth.DeleteOAuth2Application(ctx.ParamsInt64("id"), oa.OwnerID); err != nil {
|
|
|
|
ctx.ServerError("DeleteOAuth2Application", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.remove_oauth2_application_success"))
|
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{"redirect": oa.BasePathList})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RevokeGrant revokes the grant
|
|
|
|
func (oa *OAuth2CommonHandlers) RevokeGrant(ctx *context.Context) {
|
|
|
|
if err := auth.RevokeOAuth2Grant(ctx, ctx.ParamsInt64("grantId"), oa.OwnerID); err != nil {
|
|
|
|
ctx.ServerError("RevokeOAuth2Grant", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.revoke_oauth2_grant_success"))
|
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{"redirect": oa.BasePathList})
|
|
|
|
}
|