Skip to content

Commit c31df25

Browse files
committed
Implement update patch method for api
1 parent cb0e0ce commit c31df25

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

modules/structs/auth_oauth2.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ type CreateAuthOauth2Option struct {
3939

4040
// EditUserOption edit user options
4141
type EditAuthOauth2Option struct {
42-
// // required: true
43-
SourceID int64 `json:"source_id"`
44-
4542
AuthenticationName string `json:"authentication_name" binding:"Required"`
4643
ProviderIconURL string `json:"provider_icon_url"`
4744
ProviderClientID string `json:"provider_client_id" binding:"Required"`

routers/api/v1/admin/auth_oauth.go

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,6 @@ import (
2424
func CreateOauthAuth(ctx *context.APIContext) {
2525
form := web.GetForm(ctx).(*api.CreateAuthOauth2Option)
2626

27-
// ??? todo: what should I do here?
28-
var scopes []string
29-
// for _, s := range strings.Split(form.Oauth2Scopes, ",") {
30-
// s = strings.TrimSpace(s)
31-
// if s != "" {
32-
// scopes = append(scopes, s)
33-
// }
34-
// }
35-
3627
discoveryURL, err := url.Parse(form.ProviderAutoDiscoveryURL)
3728
if err != nil || (discoveryURL.Scheme != "http" && discoveryURL.Scheme != "https") {
3829
_ = fmt.Errorf("invalid Auto Discovery URL: %s (this must be a valid URL starting with http:// or https://)", form.ProviderAutoDiscoveryURL)
@@ -46,7 +37,7 @@ func CreateOauthAuth(ctx *context.APIContext) {
4637
OpenIDConnectAutoDiscoveryURL: form.ProviderAutoDiscoveryURL,
4738
CustomURLMapping: nil,
4839
IconURL: form.ProviderIconURL,
49-
Scopes: scopes,
40+
Scopes: generateScopes(),
5041
RequiredClaimName: form.RequiredClaimName,
5142
RequiredClaimValue: form.RequiredClaimValue,
5243
SkipLocalTwoFA: form.SkipLocal2FA,
@@ -75,6 +66,47 @@ func CreateOauthAuth(ctx *context.APIContext) {
7566

7667
// EditOauthAuth api for modifying a authentication method
7768
func EditOauthAuth(ctx *context.APIContext) {
69+
oauthIDString := ctx.PathParam("id")
70+
oauthID, oauthIDErr := strconv.Atoi(oauthIDString)
71+
if oauthIDErr != nil {
72+
ctx.APIErrorInternal(oauthIDErr)
73+
}
74+
75+
form := web.GetForm(ctx).(*api.CreateAuthOauth2Option)
76+
77+
config := &oauth2.Source{
78+
Provider: "openidConnect",
79+
ClientID: form.ProviderClientID,
80+
ClientSecret: form.ProviderClientSecret,
81+
OpenIDConnectAutoDiscoveryURL: form.ProviderAutoDiscoveryURL,
82+
CustomURLMapping: nil,
83+
IconURL: form.ProviderIconURL,
84+
Scopes: generateScopes(),
85+
RequiredClaimName: form.RequiredClaimName,
86+
RequiredClaimValue: form.RequiredClaimValue,
87+
SkipLocalTwoFA: form.SkipLocal2FA,
88+
89+
GroupClaimName: form.ClaimNameProvidingGroupNameForSource,
90+
RestrictedGroup: form.GroupClaimValueForRestrictedUsers,
91+
AdminGroup: form.GroupClaimValueForAdministratorUsers,
92+
GroupTeamMap: form.MapClaimedGroupsToOrganizationTeams,
93+
GroupTeamMapRemoval: form.RemoveUsersFromSyncronizedTeams,
94+
}
95+
96+
updateErr := auth_model.UpdateSource(ctx, &auth_model.Source{
97+
ID: int64(oauthID),
98+
Type: auth_model.OAuth2,
99+
Name: form.AuthenticationName,
100+
IsActive: true,
101+
Cfg: config,
102+
})
103+
104+
if updateErr != nil {
105+
ctx.APIErrorInternal(updateErr)
106+
return
107+
}
108+
109+
ctx.Status(http.StatusCreated)
78110
}
79111

80112
// DeleteOauthAuth api for deleting a authentication method
@@ -85,6 +117,17 @@ func DeleteOauthAuth(ctx *context.APIContext) {
85117
ctx.APIErrorInternal(oauthIDErr)
86118
}
87119

120+
source, sourceErr := auth_model.GetSourceByID(ctx, int64(oauthID))
121+
if sourceErr != nil {
122+
ctx.APIErrorInternal(sourceErr)
123+
return
124+
}
125+
126+
if source.Type != auth_model.OAuth2 {
127+
ctx.APIErrorNotFound()
128+
return
129+
}
130+
88131
err := auth_model.DeleteSource(ctx, int64(oauthID))
89132
if err != nil {
90133
ctx.APIErrorInternal(err)
@@ -113,3 +156,17 @@ func SearchOauthAuth(ctx *context.APIContext) {
113156
ctx.SetTotalCountHeader(maxResults)
114157
ctx.JSON(http.StatusOK, &results)
115158
}
159+
160+
// ??? todo: what should I do here?
161+
func generateScopes() []string {
162+
var scopes []string
163+
164+
// for _, s := range strings.Split(form.Oauth2Scopes, ",") {
165+
// s = strings.TrimSpace(s)
166+
// if s != "" {
167+
// scopes = append(scopes, s)
168+
// }
169+
// }
170+
171+
return scopes
172+
}

routers/api/v1/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1660,7 +1660,8 @@ func Routes() *web.Router {
16601660
m.Group("/identity-auth", func() {
16611661
m.Group("/oauth", func() {
16621662
m.Get("", admin.SearchOauthAuth)
1663-
m.Put("/new", bind(api.CreateAuthOauth2Option{}), admin.CreateOauthAuth)
1663+
m.Put("", bind(api.CreateAuthOauth2Option{}), admin.CreateOauthAuth)
1664+
m.Patch("/{id}", bind(api.EditAuthOauth2Option{}), admin.EditOauthAuth)
16641665
m.Delete("/{id}", admin.DeleteOauthAuth)
16651666
})
16661667
})

0 commit comments

Comments
 (0)