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

upgrade*: auth pkg change to public #181

Merged
merged 1 commit into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 23 additions & 5 deletions internal/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import (
mycasbin "github.com/go-atomci/atomci/internal/middleware/casbin"
"github.com/go-atomci/atomci/internal/middleware/log"

"github.com/go-atomci/atomci/internal/core/auth"
"github.com/go-atomci/atomci/internal/core/auth/ldap"
"github.com/go-atomci/atomci/internal/core/auth/local"
"github.com/go-atomci/atomci/pkg/auth"
"github.com/go-atomci/atomci/pkg/auth/ldap"
"github.com/go-atomci/atomci/pkg/auth/local"
)

// AuthController .operations about login/logout
Expand Down Expand Up @@ -68,9 +68,27 @@ func (a *AuthController) Authenticate() {
var loginProvider auth.Provider
switch req.LoginType {
case models.LocalAuth:
loginProvider = local.NewProvider()
userModel, err := dao.GetUser(req.Username)
if err != nil {
log.Log.Error("get user error: " + err.Error())
a.CustomAbort(http.StatusBadRequest, "用户不存在或密码错误")
}
loginProvider = local.NewProvider(
local.Name(userModel.Name),
local.Email(userModel.Email),
local.User(userModel.User),
local.Password(userModel.Password),
)
case models.LDAPAuth:
loginProvider = ldap.NewProvider()
port, _ := beego.AppConfig.Int("ldap::port")
loginProvider = ldap.NewProvider(
ldap.BaseDN(beego.AppConfig.String("ldap::baseDN")),
ldap.Host(beego.AppConfig.String("ldap::host")),
ldap.Port(port),
ldap.BindDN(beego.AppConfig.String("ldap::bindDN")),
ldap.BindPassword(beego.AppConfig.String("ldap::bindPassword")),
ldap.UserFilter(beego.AppConfig.String("ldap::userFilter")),
)
default:
log.Log.Error("login_type is %v, not support", req.LoginType)
http.Error(a.Ctx.ResponseWriter, "不支持此类型的登录,请联系管理员", http.StatusInternalServerError)
Expand Down
File renamed without changes.
22 changes: 16 additions & 6 deletions internal/core/auth/ldap/ldap.go → pkg/auth/ldap/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,31 @@ package ldap
import (
"fmt"

"github.com/go-atomci/atomci/internal/core/auth"
"github.com/go-atomci/atomci/internal/middleware/log"
"github.com/go-atomci/atomci/pkg/auth"

"github.com/astaxie/beego"
ldap "github.com/colynn/go-ldap-client/v3"
)

// Provider a ldap authentication provider.
// TODO: support configuration later
type Provider struct{}
type Provider struct {
baseDN string
host string
port int
bindDN string
bindPassword string
userFilter string
}

// NewProvider creates a new ldap authentication provider.
func NewProvider() auth.Provider {
return &Provider{}
func NewProvider(opts ...Option) auth.Provider {
provider := &Provider{}
for _, opt := range opts {
opt(provider)
}
return provider
}

// Authenticate ..
Expand All @@ -53,9 +64,8 @@ func (p *Provider) Authenticate(user, password string) (*auth.ExternalAccount, e
}
defer client.Close()

resp := map[string]string{}
authVerify, resp, err := client.Authenticate(user, password)
if authVerify == false {
if !authVerify {
if err != nil {
log.Log.Error("authVerify error: %s", err.Error())
}
Expand Down
39 changes: 39 additions & 0 deletions pkg/auth/ldap/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ldap

type Option func(*Provider)

func BaseDN(baseDN string) Option {
return func(p *Provider) {
p.baseDN = baseDN
}
}

func Host(host string) Option {
return func(p *Provider) {
p.host = host
}
}

func Port(port int) Option {
return func(p *Provider) {
p.port = port
}
}

func BindDN(bindDN string) Option {
return func(p *Provider) {
p.bindDN = bindDN
}
}

func BindPassword(bindPassword string) Option {
return func(p *Provider) {
p.bindPassword = bindPassword
}
}

func UserFilter(userFilter string) Option {
return func(p *Provider) {
p.userFilter = userFilter
}
}
32 changes: 17 additions & 15 deletions internal/core/auth/local/local.go → pkg/auth/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,41 @@ package local
import (
"fmt"

"github.com/go-atomci/atomci/internal/core/auth"
"github.com/go-atomci/atomci/internal/dao"
"github.com/go-atomci/atomci/internal/middleware/log"
"github.com/go-atomci/atomci/pkg/auth"

"golang.org/x/crypto/bcrypt"
)

// Provider a local authentication provider.
// TODO: support configuration later
type Provider struct{}
type Provider struct {
name string
email string
user string
password string
}

// NewProvider creates a new local authentication provider.
func NewProvider() auth.Provider {
return &Provider{}
func NewProvider(opts ...Option) auth.Provider {
provider := &Provider{}
for _, opt := range opts {
opt(provider)
}
return provider
}

// Authenticate ..
func (p *Provider) Authenticate(loginUser, password string) (*auth.ExternalAccount, error) {
userModel, err := dao.GetUser(loginUser)
if err != nil {
log.Log.Error("get user error: %v", err.Error())
return nil, fmt.Errorf("用户不存在或密码错误")
}

_, err = CompareHashAndPassword(userModel.Password, password)
_, err := CompareHashAndPassword(p.password, password)
if err != nil {
log.Log.Error("comparehas password, error: %v", err.Error())
return nil, fmt.Errorf("用户不存在或密码错误")
}
return &auth.ExternalAccount{
Name: userModel.Name,
Email: userModel.Email,
User: userModel.User,
Name: p.name,
Email: p.email,
User: p.user,
}, nil

}
Expand Down
27 changes: 27 additions & 0 deletions pkg/auth/local/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package local

type Option func(*Provider)

func Name(name string) Option {
return func(p *Provider) {
p.name = name
}
}

func Email(email string) Option {
return func(p *Provider) {
p.email = email
}
}

func User(user string) Option {
return func(p *Provider) {
p.user = user
}
}

func Password(password string) Option {
return func(p *Provider) {
p.password = password
}
}