|
5 | 5 | package setting
|
6 | 6 |
|
7 | 7 | import (
|
8 |
| - "fmt" |
9 |
| - "net/http" |
10 |
| - |
11 |
| - "code.gitea.io/gitea/models/auth" |
12 | 8 | "code.gitea.io/gitea/modules/base"
|
13 | 9 | "code.gitea.io/gitea/modules/context"
|
14 |
| - "code.gitea.io/gitea/modules/log" |
15 | 10 | "code.gitea.io/gitea/modules/setting"
|
16 |
| - "code.gitea.io/gitea/modules/web" |
17 |
| - "code.gitea.io/gitea/services/forms" |
18 | 11 | )
|
19 | 12 |
|
20 | 13 | const (
|
21 |
| - tplSettingsOAuthApplications base.TplName = "user/settings/applications_oauth2_edit" |
| 14 | + tplSettingsOAuthApplicationEdit base.TplName = "user/settings/applications_oauth2_edit" |
22 | 15 | )
|
23 | 16 |
|
| 17 | +func newOAuth2CommonHandlers(userID int64) *OAuth2CommonHandlers { |
| 18 | + return &OAuth2CommonHandlers{ |
| 19 | + OwnerID: userID, |
| 20 | + BasePathList: setting.AppSubURL + "/user/settings/applications", |
| 21 | + BasePathEditPrefix: setting.AppSubURL + "/user/settings/applications/oauth2", |
| 22 | + TplAppEdit: tplSettingsOAuthApplicationEdit, |
| 23 | + } |
| 24 | +} |
| 25 | + |
24 | 26 | // OAuthApplicationsPost response for adding a oauth2 application
|
25 | 27 | func OAuthApplicationsPost(ctx *context.Context) {
|
26 |
| - form := web.GetForm(ctx).(*forms.EditOAuth2ApplicationForm) |
27 | 28 | ctx.Data["Title"] = ctx.Tr("settings")
|
28 | 29 | ctx.Data["PageIsSettingsApplications"] = true
|
29 | 30 |
|
30 |
| - if ctx.HasError() { |
31 |
| - loadApplicationsData(ctx) |
32 |
| - |
33 |
| - ctx.HTML(http.StatusOK, tplSettingsApplications) |
34 |
| - return |
35 |
| - } |
36 |
| - // TODO validate redirect URI |
37 |
| - app, err := auth.CreateOAuth2Application(ctx, auth.CreateOAuth2ApplicationOptions{ |
38 |
| - Name: form.Name, |
39 |
| - RedirectURIs: []string{form.RedirectURI}, |
40 |
| - UserID: ctx.Doer.ID, |
41 |
| - }) |
42 |
| - if err != nil { |
43 |
| - ctx.ServerError("CreateOAuth2Application", err) |
44 |
| - return |
45 |
| - } |
46 |
| - ctx.Flash.Success(ctx.Tr("settings.create_oauth2_application_success")) |
47 |
| - ctx.Data["App"] = app |
48 |
| - ctx.Data["ClientSecret"], err = app.GenerateClientSecret() |
49 |
| - if err != nil { |
50 |
| - ctx.ServerError("GenerateClientSecret", err) |
51 |
| - return |
52 |
| - } |
53 |
| - ctx.HTML(http.StatusOK, tplSettingsOAuthApplications) |
| 31 | + oa := newOAuth2CommonHandlers(ctx.Doer.ID) |
| 32 | + oa.AddApp(ctx) |
54 | 33 | }
|
55 | 34 |
|
56 | 35 | // OAuthApplicationsEdit response for editing oauth2 application
|
57 | 36 | func OAuthApplicationsEdit(ctx *context.Context) {
|
58 |
| - form := web.GetForm(ctx).(*forms.EditOAuth2ApplicationForm) |
59 | 37 | ctx.Data["Title"] = ctx.Tr("settings")
|
60 | 38 | ctx.Data["PageIsSettingsApplications"] = true
|
61 | 39 |
|
62 |
| - if ctx.HasError() { |
63 |
| - loadApplicationsData(ctx) |
64 |
| - |
65 |
| - ctx.HTML(http.StatusOK, tplSettingsApplications) |
66 |
| - return |
67 |
| - } |
68 |
| - // TODO validate redirect URI |
69 |
| - var err error |
70 |
| - if ctx.Data["App"], err = auth.UpdateOAuth2Application(auth.UpdateOAuth2ApplicationOptions{ |
71 |
| - ID: ctx.ParamsInt64("id"), |
72 |
| - Name: form.Name, |
73 |
| - RedirectURIs: []string{form.RedirectURI}, |
74 |
| - UserID: ctx.Doer.ID, |
75 |
| - }); err != nil { |
76 |
| - ctx.ServerError("UpdateOAuth2Application", err) |
77 |
| - return |
78 |
| - } |
79 |
| - ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success")) |
80 |
| - ctx.HTML(http.StatusOK, tplSettingsOAuthApplications) |
| 40 | + oa := newOAuth2CommonHandlers(ctx.Doer.ID) |
| 41 | + oa.EditSave(ctx) |
81 | 42 | }
|
82 | 43 |
|
83 | 44 | // OAuthApplicationsRegenerateSecret handles the post request for regenerating the secret
|
84 | 45 | func OAuthApplicationsRegenerateSecret(ctx *context.Context) {
|
85 | 46 | ctx.Data["Title"] = ctx.Tr("settings")
|
86 | 47 | ctx.Data["PageIsSettingsApplications"] = true
|
87 | 48 |
|
88 |
| - app, err := auth.GetOAuth2ApplicationByID(ctx, ctx.ParamsInt64("id")) |
89 |
| - if err != nil { |
90 |
| - if auth.IsErrOAuthApplicationNotFound(err) { |
91 |
| - ctx.NotFound("Application not found", err) |
92 |
| - return |
93 |
| - } |
94 |
| - ctx.ServerError("GetOAuth2ApplicationByID", err) |
95 |
| - return |
96 |
| - } |
97 |
| - if app.UID != ctx.Doer.ID { |
98 |
| - ctx.NotFound("Application not found", nil) |
99 |
| - return |
100 |
| - } |
101 |
| - ctx.Data["App"] = app |
102 |
| - ctx.Data["ClientSecret"], err = app.GenerateClientSecret() |
103 |
| - if err != nil { |
104 |
| - ctx.ServerError("GenerateClientSecret", err) |
105 |
| - return |
106 |
| - } |
107 |
| - ctx.Flash.Success(ctx.Tr("settings.update_oauth2_application_success")) |
108 |
| - ctx.HTML(http.StatusOK, tplSettingsOAuthApplications) |
| 49 | + oa := newOAuth2CommonHandlers(ctx.Doer.ID) |
| 50 | + oa.RegenerateSecret(ctx) |
109 | 51 | }
|
110 | 52 |
|
111 | 53 | // OAuth2ApplicationShow displays the given application
|
112 | 54 | func OAuth2ApplicationShow(ctx *context.Context) {
|
113 |
| - app, err := auth.GetOAuth2ApplicationByID(ctx, ctx.ParamsInt64("id")) |
114 |
| - if err != nil { |
115 |
| - if auth.IsErrOAuthApplicationNotFound(err) { |
116 |
| - ctx.NotFound("Application not found", err) |
117 |
| - return |
118 |
| - } |
119 |
| - ctx.ServerError("GetOAuth2ApplicationByID", err) |
120 |
| - return |
121 |
| - } |
122 |
| - if app.UID != ctx.Doer.ID { |
123 |
| - ctx.NotFound("Application not found", nil) |
124 |
| - return |
125 |
| - } |
126 |
| - ctx.Data["App"] = app |
127 |
| - ctx.HTML(http.StatusOK, tplSettingsOAuthApplications) |
| 55 | + oa := newOAuth2CommonHandlers(ctx.Doer.ID) |
| 56 | + oa.EditShow(ctx) |
128 | 57 | }
|
129 | 58 |
|
130 | 59 | // DeleteOAuth2Application deletes the given oauth2 application
|
131 | 60 | func DeleteOAuth2Application(ctx *context.Context) {
|
132 |
| - if err := auth.DeleteOAuth2Application(ctx.FormInt64("id"), ctx.Doer.ID); err != nil { |
133 |
| - ctx.ServerError("DeleteOAuth2Application", err) |
134 |
| - return |
135 |
| - } |
136 |
| - log.Trace("OAuth2 Application deleted: %s", ctx.Doer.Name) |
137 |
| - |
138 |
| - ctx.Flash.Success(ctx.Tr("settings.remove_oauth2_application_success")) |
139 |
| - ctx.JSON(http.StatusOK, map[string]interface{}{ |
140 |
| - "redirect": setting.AppSubURL + "/user/settings/applications", |
141 |
| - }) |
| 61 | + oa := newOAuth2CommonHandlers(ctx.Doer.ID) |
| 62 | + oa.DeleteApp(ctx) |
142 | 63 | }
|
143 | 64 |
|
144 | 65 | // RevokeOAuth2Grant revokes the grant with the given id
|
145 | 66 | func RevokeOAuth2Grant(ctx *context.Context) {
|
146 |
| - if ctx.Doer.ID == 0 || ctx.FormInt64("id") == 0 { |
147 |
| - ctx.ServerError("RevokeOAuth2Grant", fmt.Errorf("user id or grant id is zero")) |
148 |
| - return |
149 |
| - } |
150 |
| - if err := auth.RevokeOAuth2Grant(ctx, ctx.FormInt64("id"), ctx.Doer.ID); err != nil { |
151 |
| - ctx.ServerError("RevokeOAuth2Grant", err) |
152 |
| - return |
153 |
| - } |
154 |
| - |
155 |
| - ctx.Flash.Success(ctx.Tr("settings.revoke_oauth2_grant_success")) |
156 |
| - ctx.JSON(http.StatusOK, map[string]interface{}{ |
157 |
| - "redirect": setting.AppSubURL + "/user/settings/applications", |
158 |
| - }) |
| 67 | + oa := newOAuth2CommonHandlers(ctx.Doer.ID) |
| 68 | + oa.RevokeGrant(ctx) |
159 | 69 | }
|
0 commit comments