* add logout v2 for sso

* support sms-code login

* use db instead of memory cache for login code
master
yubo 4 years ago committed by GitHub
parent e63e741ad6
commit 1fdcbd848c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1 @@
您好,您的登录验证码为 {{.Code}}

@ -0,0 +1 @@
您好,您的登录验证码为 {{.Code}}

@ -284,3 +284,18 @@ CREATE TABLE `operation_log`
KEY (`res_cl`, `res_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
CREATE TABLE `login_code`
(
`username` varchar(64) not null comment 'login name, cannot rename',
`code` varchar(32) not null,
`login_type` varchar(32) not null,
`created_at` bigint not null comment 'created at',
KEY (`code`),
KEY (`created_at`),
UNIQUE KEY (`username`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;

@ -0,0 +1,33 @@
package models
type LoginCode struct {
Username string `json:"username"`
Code string `json:"code"`
LoginType string `json:"login_type"`
CreatedAt int64 `json:"created_at"`
}
func LoginCodeGet(where string, args ...interface{}) (*LoginCode, error) {
var obj LoginCode
has, err := DB["rdb"].Where(where, args...).Get(&obj)
if err != nil {
return nil, err
}
if !has {
return nil, nil
}
return &obj, nil
}
func (p *LoginCode) Save() error {
p.Del()
_, err := DB["rdb"].Insert(p)
return err
}
func (p *LoginCode) Del() error {
_, err := DB["rdb"].Where("username=?", p.Username).Delete(new(LoginCode))
return err
}

@ -24,6 +24,7 @@ func Config(r *gin.Engine) {
notLogin.GET("/auth/v2/authorize", authAuthorizeV2)
notLogin.GET("/auth/v2/callback", authCallbackV2)
notLogin.GET("/auth/v2/logout", logoutV2)
}
hbs := r.Group("/api/hbs")
@ -170,5 +171,7 @@ func Config(r *gin.Engine) {
v1.GET("/users", userListGet)
v1.POST("/login", v1Login)
v1.POST("/send-login-code-by-sms", v1SendLoginCodeBySms)
v1.POST("/send-login-code-by-email", v1SendLoginCodeByEmail)
}
}

File diff suppressed because it is too large Load Diff

@ -86,6 +86,7 @@ func InitSSO() {
func Authorize(redirect string) string {
state := uuid.New().String()
cli.cache.Add(state, redirect, cli.stateExpiresIn)
// log.Printf("add state %s", state)
return cli.config.AuthCodeURL(state)
}
@ -104,15 +105,16 @@ func Callback(code, state string) (string, *models.User, error) {
return "", nil, fmt.Errorf("invalid state %s", state)
}
cli.cache.Remove(state)
// log.Printf("remove state %s", state)
redirect := s.(string)
log.Printf("callback, get state %s redirect %s", state, redirect)
// log.Printf("callback, get state %s redirect %s", state, redirect)
u, err := exchangeUser(code)
if err != nil {
return "", nil, err
}
log.Printf("exchange user %v", u)
// log.Printf("exchange user %v", u)
user, err := models.UserGet("username=?", u.Username)
if err != nil {

Loading…
Cancel
Save