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

Create admin auths API #14808

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
36 changes: 36 additions & 0 deletions modules/convert/auth_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package convert

import (
"code.gitea.io/gitea/models"
api "code.gitea.io/gitea/modules/structs"
)

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
}

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
}
38 changes: 38 additions & 0 deletions modules/structs/auth_source.go
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"`
}
109 changes: 109 additions & 0 deletions routers/api/v1/admin/auths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package admin

import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
6543 marked this conversation as resolved.
Show resolved Hide resolved
"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"
"net/http"
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 = 0
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)
}
4 changes: 4 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,10 @@ func Routes() *web.Route {
}, orgAssignment(false, true), reqToken(), reqTeamMembership())

m.Group("/admin", func() {
m.Group("/auths", func() {
m.Get("", admin.ListAuthSources)
m.Post("", bind(api.CreateAuthSource{}), admin.CreateAuthSource)
})
m.Group("/cron", func() {
m.Get("", admin.ListCronTasks)
m.Post("/{task}", admin.PostCronTask)
Expand Down
24 changes: 24 additions & 0 deletions routers/api/v1/swagger/auth_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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
}
Loading