Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creation of OAuth2 applications for orgs #18084

Merged
merged 52 commits into from
Oct 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
840d1e0
Allow creation of OAuth2 applications for orgs
qwerty287 Dec 23, 2021
bdda6a1
Merge branch 'main' into org-oauth2
qwerty287 Dec 23, 2021
5dd629c
Merge branch 'main' into org-oauth2
qwerty287 Dec 24, 2021
e844523
Merge branch 'main' into org-oauth2
qwerty287 Jan 4, 2022
2e3bccf
Fix import
qwerty287 Jan 7, 2022
d52f718
Merge branch 'main' into org-oauth2
qwerty287 Jan 7, 2022
171701a
fmt
qwerty287 Jan 7, 2022
8bb9912
Merge branch 'main' into org-oauth2
qwerty287 Jan 22, 2022
4567ea9
Merge branch 'main' into org-oauth2
qwerty287 Feb 14, 2022
7e5c6dc
Merge branch 'main' into org-oauth2
qwerty287 Feb 19, 2022
5940d7f
Merge branch 'main' into org-oauth2
qwerty287 Mar 21, 2022
86e733c
Merge branch 'main' into org-oauth2
qwerty287 Mar 27, 2022
a8dfb7b
Merge branch 'org-oauth2' of github.com:qwerty287/gitea into org-oauth2
qwerty287 Mar 27, 2022
1f920df
Fix merge
qwerty287 Mar 27, 2022
3775a32
Merge branch 'main' into org-oauth2
qwerty287 Mar 27, 2022
e4fe3c6
Merge branch 'main' into org-oauth2
qwerty287 Apr 5, 2022
cb9c5aa
Merge branch 'main' into org-oauth2
qwerty287 Apr 15, 2022
2804d63
Merge branch 'main' into org-oauth2
qwerty287 Apr 28, 2022
4f6579e
Merge branch 'main' into org-oauth2
qwerty287 May 4, 2022
0d53655
Merge branch 'main' into org-oauth2
qwerty287 Jun 5, 2022
c04065b
Adapt refactors
qwerty287 Jun 5, 2022
97b795d
Merge branch 'main' into org-oauth2
qwerty287 Jun 5, 2022
443b1bb
Merge branch 'main' into org-oauth2
qwerty287 Jun 5, 2022
6d9650e
Merge branch 'main' into org-oauth2
qwerty287 Jun 16, 2022
0d76da1
Merge branch 'main' into org-oauth2
qwerty287 Jul 18, 2022
c1fd02a
Merge branch 'main' into org-oauth2
qwerty287 Aug 15, 2022
eb58018
Merge branch 'main' into org-oauth2
qwerty287 Aug 31, 2022
e59ecb4
Merge branch 'main' into org-oauth2
qwerty287 Sep 25, 2022
fe95171
Use `locale.Tr`
qwerty287 Sep 26, 2022
5805461
Merge branch 'main' into org-oauth2
qwerty287 Sep 26, 2022
09c2a7e
Merge branch 'main' into org-oauth2
qwerty287 Sep 27, 2022
dbdd8c5
Merge branch 'main' into org-oauth2
6543 Sep 28, 2022
599ef41
Merge branch 'main' into org-oauth2
6543 Sep 29, 2022
975a01d
Merge branch 'main' into org-oauth2
qwerty287 Sep 29, 2022
64b1b9b
Merge branch 'main' into org-oauth2
6543 Sep 29, 2022
48f9907
Merge branch 'main' into org-oauth2
qwerty287 Sep 29, 2022
cfa7bae
Merge branch 'main' into org-oauth2
qwerty287 Oct 2, 2022
dcfed4b
Merge branch 'main' into org-oauth2
qwerty287 Oct 3, 2022
16d6a90
Remove modal
qwerty287 Oct 3, 2022
4be12c4
Merge branch 'main' into org-oauth2
6543 Oct 3, 2022
20183ef
Merge branch 'main' into org-oauth2
qwerty287 Oct 4, 2022
4031fa8
Remove dupl code on tmpls
qwerty287 Oct 5, 2022
6facd1e
Fixes and move edit form to separate tmpl
qwerty287 Oct 5, 2022
cafd775
fix incent
qwerty287 Oct 5, 2022
57595d5
Merge branch 'main' into org-oauth2
qwerty287 Oct 5, 2022
280bcef
refactor
wxiaoguang Oct 6, 2022
95d7686
fix org application url
wxiaoguang Oct 6, 2022
c512332
fix links
wxiaoguang Oct 6, 2022
e385e92
fix lint
wxiaoguang Oct 6, 2022
6c4770d
Merge branch 'main' into org-oauth2
wxiaoguang Oct 6, 2022
dc778a4
Merge branch 'main' into org-oauth2
wxiaoguang Oct 7, 2022
f95470f
Merge branch 'main' into org-oauth2
wxiaoguang Oct 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions assets/go-licenses.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions models/auth/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ func updateOAuth2Application(ctx context.Context, app *OAuth2Application) error

func deleteOAuth2Application(ctx context.Context, id, userid int64) error {
sess := db.GetEngine(ctx)
if deleted, err := sess.Delete(&OAuth2Application{ID: id, UID: userid}); err != nil {
// the userid could be 0 if the app is instance-wide
if deleted, err := sess.Where(builder.Eq{"id": id, "uid": userid}).Delete(&OAuth2Application{}); err != nil {
return err
} else if deleted == 0 {
return ErrOAuthApplicationNotFound{ID: id}
Expand Down Expand Up @@ -476,7 +477,7 @@ func GetOAuth2GrantsByUserID(ctx context.Context, uid int64) ([]*OAuth2Grant, er

// RevokeOAuth2Grant deletes the grant with grantID and userID
func RevokeOAuth2Grant(ctx context.Context, grantID, userID int64) error {
_, err := db.DeleteByBean(ctx, &OAuth2Grant{ID: grantID, UserID: userID})
_, err := db.GetEngine(ctx).Where(builder.Eq{"id": grantID, "user_id": userID}).Delete(&OAuth2Grant{})
return err
}

Expand Down
93 changes: 93 additions & 0 deletions routers/web/org/setting_oauth2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2022 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 org

import (
"fmt"
"net/http"

"code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
user_setting "code.gitea.io/gitea/routers/web/user/setting"
)

const (
tplSettingsApplications base.TplName = "org/settings/applications"
tplSettingsOAuthApplicationEdit base.TplName = "org/settings/applications_oauth2_edit"
)

func newOAuth2CommonHandlers(org *context.Organization) *user_setting.OAuth2CommonHandlers {
return &user_setting.OAuth2CommonHandlers{
OwnerID: org.Organization.ID,
BasePathList: fmt.Sprintf("%s/org/%s/settings/applications", setting.AppSubURL, org.Organization.Name),
BasePathEditPrefix: fmt.Sprintf("%s/org/%s/settings/applications/oauth2", setting.AppSubURL, org.Organization.Name),
TplAppEdit: tplSettingsOAuthApplicationEdit,
}
}

// Applications render org applications page (for org, at the moment, there are only OAuth2 applications)
func Applications(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings.applications")
ctx.Data["PageIsOrgSettings"] = true
ctx.Data["PageIsSettingsApplications"] = true

apps, err := auth.GetOAuth2ApplicationsByUserID(ctx, ctx.Org.Organization.ID)
if err != nil {
ctx.ServerError("GetOAuth2ApplicationsByUserID", err)
return
}
ctx.Data["Applications"] = apps

ctx.HTML(http.StatusOK, tplSettingsApplications)
}

// OAuthApplicationsPost response for adding an oauth2 application
func OAuthApplicationsPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings.applications")
ctx.Data["PageIsOrgSettings"] = true
ctx.Data["PageIsSettingsApplications"] = true

oa := newOAuth2CommonHandlers(ctx.Org)
oa.AddApp(ctx)
}

// OAuth2ApplicationShow displays the given application
func OAuth2ApplicationShow(ctx *context.Context) {
ctx.Data["PageIsOrgSettings"] = true
ctx.Data["PageIsSettingsApplications"] = true

oa := newOAuth2CommonHandlers(ctx.Org)
oa.EditShow(ctx)
}

// OAuth2ApplicationEdit response for editing oauth2 application
func OAuth2ApplicationEdit(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings.applications")
ctx.Data["PageIsOrgSettings"] = true
ctx.Data["PageIsSettingsApplications"] = true

oa := newOAuth2CommonHandlers(ctx.Org)
oa.EditSave(ctx)
}

// OAuthApplicationsRegenerateSecret handles the post request for regenerating the secret
func OAuthApplicationsRegenerateSecret(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsOrgSettings"] = true
ctx.Data["PageIsSettingsApplications"] = true

oa := newOAuth2CommonHandlers(ctx.Org)
oa.RegenerateSecret(ctx)
}

// DeleteOAuth2Application deletes the given oauth2 application
func DeleteOAuth2Application(ctx *context.Context) {
oa := newOAuth2CommonHandlers(ctx.Org)
oa.DeleteApp(ctx)
}

// TODO: revokes the grant with the given id
134 changes: 22 additions & 112 deletions routers/web/user/setting/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,155 +5,65 @@
package setting

import (
"fmt"
"net/http"

"code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/services/forms"
)

const (
tplSettingsOAuthApplications base.TplName = "user/settings/applications_oauth2_edit"
tplSettingsOAuthApplicationEdit base.TplName = "user/settings/applications_oauth2_edit"
)

func newOAuth2CommonHandlers(userID int64) *OAuth2CommonHandlers {
return &OAuth2CommonHandlers{
OwnerID: userID,
BasePathList: setting.AppSubURL + "/user/settings/applications",
BasePathEditPrefix: setting.AppSubURL + "/user/settings/applications/oauth2",
TplAppEdit: tplSettingsOAuthApplicationEdit,
}
}

// OAuthApplicationsPost response for adding a oauth2 application
func OAuthApplicationsPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.EditOAuth2ApplicationForm)
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsApplications"] = true

if ctx.HasError() {
loadApplicationsData(ctx)

ctx.HTML(http.StatusOK, tplSettingsApplications)
return
}
// TODO validate redirect URI
app, err := auth.CreateOAuth2Application(ctx, auth.CreateOAuth2ApplicationOptions{
Name: form.Name,
RedirectURIs: []string{form.RedirectURI},
UserID: ctx.Doer.ID,
})
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
}
ctx.HTML(http.StatusOK, tplSettingsOAuthApplications)
oa := newOAuth2CommonHandlers(ctx.Doer.ID)
oa.AddApp(ctx)
}

// OAuthApplicationsEdit response for editing oauth2 application
func OAuthApplicationsEdit(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.EditOAuth2ApplicationForm)
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsApplications"] = true

if ctx.HasError() {
loadApplicationsData(ctx)

ctx.HTML(http.StatusOK, tplSettingsApplications)
return
}
// TODO validate redirect URI
var err error
if ctx.Data["App"], err = auth.UpdateOAuth2Application(auth.UpdateOAuth2ApplicationOptions{
ID: ctx.ParamsInt64("id"),
Name: form.Name,
RedirectURIs: []string{form.RedirectURI},
UserID: ctx.Doer.ID,
}); err != nil {
ctx.ServerError("UpdateOAuth2Application", err)
return
}
ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success"))
ctx.HTML(http.StatusOK, tplSettingsOAuthApplications)
oa := newOAuth2CommonHandlers(ctx.Doer.ID)
oa.EditSave(ctx)
}

// OAuthApplicationsRegenerateSecret handles the post request for regenerating the secret
func OAuthApplicationsRegenerateSecret(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsApplications"] = true

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 != ctx.Doer.ID {
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"))
ctx.HTML(http.StatusOK, tplSettingsOAuthApplications)
oa := newOAuth2CommonHandlers(ctx.Doer.ID)
oa.RegenerateSecret(ctx)
}

// OAuth2ApplicationShow displays the given application
func OAuth2ApplicationShow(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 != ctx.Doer.ID {
ctx.NotFound("Application not found", nil)
return
}
ctx.Data["App"] = app
ctx.HTML(http.StatusOK, tplSettingsOAuthApplications)
oa := newOAuth2CommonHandlers(ctx.Doer.ID)
oa.EditShow(ctx)
}

// DeleteOAuth2Application deletes the given oauth2 application
func DeleteOAuth2Application(ctx *context.Context) {
if err := auth.DeleteOAuth2Application(ctx.FormInt64("id"), ctx.Doer.ID); err != nil {
ctx.ServerError("DeleteOAuth2Application", err)
return
}
log.Trace("OAuth2 Application deleted: %s", ctx.Doer.Name)

ctx.Flash.Success(ctx.Tr("settings.remove_oauth2_application_success"))
ctx.JSON(http.StatusOK, map[string]interface{}{
"redirect": setting.AppSubURL + "/user/settings/applications",
})
oa := newOAuth2CommonHandlers(ctx.Doer.ID)
oa.DeleteApp(ctx)
}

// RevokeOAuth2Grant revokes the grant with the given id
func RevokeOAuth2Grant(ctx *context.Context) {
if ctx.Doer.ID == 0 || ctx.FormInt64("id") == 0 {
ctx.ServerError("RevokeOAuth2Grant", fmt.Errorf("user id or grant id is zero"))
return
}
if err := auth.RevokeOAuth2Grant(ctx, ctx.FormInt64("id"), ctx.Doer.ID); 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": setting.AppSubURL + "/user/settings/applications",
})
oa := newOAuth2CommonHandlers(ctx.Doer.ID)
oa.RevokeGrant(ctx)
}
Loading