-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Create admin auths API #14808
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
Closed
daniil-pankratov
wants to merge
4
commits into
go-gitea:main
from
daniil-pankratov:create-admin-auths-api
Closed
Create admin auths API #14808
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2021 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 convert | ||
|
||
import ( | ||
"code.gitea.io/gitea/models" | ||
api "code.gitea.io/gitea/modules/structs" | ||
) | ||
|
||
// ToAuthSources convert a list of models.LoginSource to a list of api.AuthSource | ||
func ToAuthSources(sources []*models.LoginSource) ([]*api.AuthSource, error) { | ||
result := make([]*api.AuthSource, len(sources)) | ||
for i, source := range sources { | ||
authSource, err := ToAuthSource(source) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result[i] = authSource | ||
} | ||
return result, nil | ||
} | ||
|
||
// ToAuthSource convert a models.LoginSource to a api.AuthSource | ||
func ToAuthSource(source *models.LoginSource) (*api.AuthSource, error) { | ||
cfg, err := source.Cfg.ToDB() | ||
if err != nil { | ||
return nil, err | ||
} | ||
authSource := &api.AuthSource{ | ||
ID: source.ID, | ||
Name: source.Name, | ||
Type: models.LoginNames[source.Type], | ||
IsActive: source.IsActived, | ||
IsSyncEnabled: source.IsSyncEnabled, | ||
CreatedTime: source.CreatedUnix.AsTime(), | ||
UpdatedTime: source.UpdatedUnix.AsTime(), | ||
Cfg: cfg, | ||
} | ||
return authSource, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2021 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 structs | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
) | ||
|
||
// AuthSource represents an authentication source | ||
type AuthSource struct { | ||
ID int64 `json:"id"` | ||
Name string `json:"name"` | ||
// enum: LDAP (via BindDN),LDAP (simple auth),SMTP,PAM,OAuth2,SPNEGO with SSPI | ||
Type string `json:"type"` | ||
IsActive bool `json:"isActive"` | ||
IsSyncEnabled bool `json:"isSyncEnabled"` | ||
// swagger:strfmt date-time | ||
CreatedTime time.Time `json:"createdTime"` | ||
// swagger:strfmt date-time | ||
UpdatedTime time.Time `json:"updatedTime"` | ||
Cfg json.RawMessage `json:"config"` | ||
} | ||
|
||
// CreateAuthSource represents an authentication source creation request | ||
type CreateAuthSource struct { | ||
// required: true | ||
Name string `json:"name" binding:"Required"` | ||
// required: true | ||
// enum: LDAP (via BindDN),LDAP (simple auth),SMTP,PAM,OAuth2,SPNEGO with SSPI | ||
Type string `json:"type" binding:"Required"` | ||
IsActive bool `json:"isActive"` | ||
IsSyncEnabled bool `json:"isSyncEnabled"` | ||
// required: true | ||
Cfg json.RawMessage `json:"config" binding:"Required"` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2021 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 admin | ||
|
||
import ( | ||
"net/http" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/context" | ||
"code.gitea.io/gitea/modules/convert" | ||
api "code.gitea.io/gitea/modules/structs" | ||
"code.gitea.io/gitea/modules/timeutil" | ||
"code.gitea.io/gitea/modules/web" | ||
|
||
xorm "xorm.io/xorm/convert" | ||
) | ||
|
||
// ListAuthSources returns list of existing authentication sources | ||
func ListAuthSources(ctx *context.APIContext) { | ||
// swagger:operation GET /admin/auths admin adminAuthsSourcesList | ||
// --- | ||
// summary: List existing authentication sources | ||
// produces: | ||
// - application/json | ||
// responses: | ||
// "200": | ||
// "$ref": "#/responses/AuthSourcesList" | ||
// "403": | ||
// "$ref": "#/responses/forbidden" | ||
sources, err := models.LoginSources() | ||
if err != nil { | ||
ctx.InternalServerError(err) | ||
return | ||
} | ||
result, err := convert.ToAuthSources(sources) | ||
if err != nil { | ||
ctx.InternalServerError(err) | ||
return | ||
} | ||
ctx.JSON(http.StatusOK, result) | ||
} | ||
|
||
// CreateAuthSource creates new authentication source | ||
func CreateAuthSource(ctx *context.APIContext) { | ||
// swagger:operation POST /admin/auths admin adminCreateAuthSource | ||
// --- | ||
// summary: Create new authentication source | ||
// consumes: | ||
// - application/json | ||
// produces: | ||
// - application/json | ||
// parameters: | ||
// - name: body | ||
// in: body | ||
// schema: | ||
// "$ref": "#/definitions/CreateAuthSource" | ||
// responses: | ||
// "201": | ||
// "$ref": "#/responses/AuthSource" | ||
// "403": | ||
// "$ref": "#/responses/forbidden" | ||
authSource := web.GetForm(ctx).(*api.CreateAuthSource) | ||
var config xorm.Conversion | ||
var loginType models.LoginType | ||
for key, val := range models.LoginNames { | ||
if authSource.Type == val { | ||
loginType = key | ||
switch key { | ||
case models.LoginLDAP: | ||
config = &models.LDAPConfig{} | ||
case models.LoginSMTP: | ||
config = &models.SMTPConfig{} | ||
case models.LoginPAM: | ||
config = &models.PAMConfig{} | ||
case models.LoginDLDAP: | ||
config = &models.LDAPConfig{} | ||
case models.LoginOAuth2: | ||
config = &models.OAuth2Config{} | ||
case models.LoginSSPI: | ||
config = &models.SSPIConfig{} | ||
} | ||
break | ||
} | ||
} | ||
if loginType == 0 { | ||
ctx.Error(http.StatusBadRequest, "", "Authentication source type is invalid") | ||
return | ||
} | ||
if err := config.FromDB(authSource.Cfg); err != nil { | ||
ctx.InternalServerError(err) | ||
return | ||
} | ||
|
||
source := &models.LoginSource{ | ||
Type: loginType, | ||
Cfg: config, | ||
Name: authSource.Name, | ||
IsActived: authSource.IsActive, | ||
IsSyncEnabled: authSource.IsSyncEnabled, | ||
CreatedUnix: timeutil.TimeStampNow(), | ||
UpdatedUnix: timeutil.TimeStampNow(), | ||
} | ||
if err := models.CreateLoginSource(source); err != nil { | ||
ctx.InternalServerError(err) | ||
return | ||
} | ||
result, err := convert.ToAuthSource(source) | ||
if err != nil { | ||
ctx.InternalServerError(err) | ||
return | ||
} | ||
ctx.JSON(http.StatusCreated, result) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2021 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 swagger | ||
|
||
import api "code.gitea.io/gitea/modules/structs" | ||
|
||
// AuthSourcesList | ||
// swagger:response AuthSourcesList | ||
type swaggerAuthSourcesList struct { | ||
// in:body | ||
Body []api.AuthSource `json:"body"` | ||
} | ||
|
||
// AuthSource | ||
// swagger:response AuthSource | ||
type swaggerAuthSource struct { | ||
// in:body | ||
Body api.AuthSource `json:"body"` | ||
} | ||
|
||
// CreateAuthSource | ||
// swagger:response CreateAuthSource | ||
type swaggerCreateAuthSource struct { | ||
// in:body | ||
CreateAuthSource api.CreateAuthSource | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.