|
| 1 | +// Copyright 2015 The Gogs Authors. All rights reserved. |
| 2 | +// Copyright 2019 The Gitea Authors. All rights reserved. |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +package admin |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + |
| 12 | + auth_model "code.gitea.io/gitea/models/auth" |
| 13 | + api "code.gitea.io/gitea/modules/structs" |
| 14 | + |
| 15 | + "code.gitea.io/gitea/modules/web" |
| 16 | + "code.gitea.io/gitea/services/auth/source/oauth2" |
| 17 | + "code.gitea.io/gitea/services/context" |
| 18 | +) |
| 19 | + |
| 20 | +// CreateOauthAuth create a new external authentication for oauth2 |
| 21 | +func CreateOauthAuth(ctx *context.APIContext) { |
| 22 | + form := web.GetForm(ctx).(*api.CreateAuthOauth2Option) |
| 23 | + |
| 24 | + var scopes []string |
| 25 | + // for _, s := range strings.Split(form.Oauth2Scopes, ",") { |
| 26 | + // s = strings.TrimSpace(s) |
| 27 | + // if s != "" { |
| 28 | + // scopes = append(scopes, s) |
| 29 | + // } |
| 30 | + // } |
| 31 | + |
| 32 | + discoveryURL, err := url.Parse(form.ProviderAutoDiscoveryURL) |
| 33 | + if err != nil || (discoveryURL.Scheme != "http" && discoveryURL.Scheme != "https") { |
| 34 | + fmt.Errorf("invalid Auto Discovery URL: %s (this must be a valid URL starting with http:// or https://)", form.ProviderAutoDiscoveryURL) |
| 35 | + |
| 36 | + // todo: implement handling |
| 37 | + } |
| 38 | + |
| 39 | + config := &oauth2.Source{ |
| 40 | + Provider: "openidConnect", |
| 41 | + ClientID: form.ProviderClientID, |
| 42 | + ClientSecret: form.ProviderClientSecret, |
| 43 | + OpenIDConnectAutoDiscoveryURL: form.ProviderAutoDiscoveryURL, |
| 44 | + CustomURLMapping: nil, |
| 45 | + IconURL: form.ProviderIconURL, |
| 46 | + Scopes: scopes, |
| 47 | + RequiredClaimName: form.RequiredClaimName, |
| 48 | + RequiredClaimValue: form.RequiredClaimValue, |
| 49 | + SkipLocalTwoFA: form.SkipLocal2FA, |
| 50 | + |
| 51 | + GroupClaimName: form.ClaimNameProvidingGroupNameForSource, |
| 52 | + RestrictedGroup: form.GroupClaimValueForRestrictedUsers, |
| 53 | + AdminGroup: form.GroupClaimValueForAdministratorUsers, |
| 54 | + GroupTeamMap: form.MapClaimedGroupsToOrganizationTeams, |
| 55 | + GroupTeamMapRemoval: form.RemoveUsersFromSyncronizedTeams, |
| 56 | + } |
| 57 | + |
| 58 | + auth_model.CreateSource(ctx, &auth_model.Source{ |
| 59 | + Type: auth_model.OAuth2, |
| 60 | + Name: form.AuthenticationName, |
| 61 | + IsActive: true, |
| 62 | + Cfg: config, |
| 63 | + }) |
| 64 | + |
| 65 | + ctx.Status(http.StatusCreated) |
| 66 | + |
| 67 | + // ctx.JSON(http.StatusCreated, convert.ToUser(ctx, u, ctx.Doer)) |
| 68 | +} |
| 69 | + |
| 70 | +// EditOauthAuth api for modifying a authentication method |
| 71 | +func EditOauthAuth(ctx *context.APIContext) { |
| 72 | +} |
| 73 | + |
| 74 | +// DeleteOauthAuth api for deleting a authentication method |
| 75 | +func DeleteOauthAuth(ctx *context.APIContext) { |
| 76 | +} |
| 77 | + |
| 78 | +// // SearchOauthAuth API for getting information of the configured authentication methods according the filter conditions |
| 79 | +func SearchOauthAuth(ctx *context.APIContext) { |
| 80 | + |
| 81 | +} |
0 commit comments