forked from pneymrl2f/nightingale
login with sso,captcha,sms-code (#374)
* add logout v2 for sso * support sms-code login * use db instead of memory cache for login code * feature: support reset password by sms code * remove deprecated api/code * feature: support image captcha * use db instead of memory cache for sso.auth.statemaster
parent
7999c1fbe5
commit
9bef8ddee3
@ -0,0 +1,42 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type AuthState struct {
|
||||
State string `json:"state"`
|
||||
Typ string `json:"typ"`
|
||||
Redirect string `json:"redirect"`
|
||||
ExpiresAt int64 `json:"expiresAt"`
|
||||
}
|
||||
|
||||
func AuthStateGet(where string, args ...interface{}) (*AuthState, error) {
|
||||
var obj AuthState
|
||||
has, err := DB["rdb"].Where(where, args...).Get(&obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !has {
|
||||
return nil, errors.New("auth state not found")
|
||||
}
|
||||
|
||||
return &obj, nil
|
||||
}
|
||||
|
||||
func (p *AuthState) Save() error {
|
||||
_, err := DB["rdb"].Insert(p)
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *AuthState) Del() error {
|
||||
_, err := DB["rdb"].Where("state=?", p.State).Delete(new(AuthState))
|
||||
return err
|
||||
}
|
||||
|
||||
func (p AuthState) CleanUp() error {
|
||||
_, err := DB["rdb"].Exec("delete from auth_state where expires_at < ?", time.Now().Unix())
|
||||
return err
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Captcha struct {
|
||||
CaptchaId string `json:"captchaId"`
|
||||
Answer string `json:"-"`
|
||||
Image string `xorm:"-" json:"image"`
|
||||
CreatedAt int64 `json:"createdAt"`
|
||||
}
|
||||
|
||||
func CaptchaGet(where string, args ...interface{}) (*Captcha, error) {
|
||||
var obj Captcha
|
||||
has, err := DB["rdb"].Where(where, args...).Get(&obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !has {
|
||||
return nil, errors.New("captcha not found")
|
||||
}
|
||||
|
||||
return &obj, nil
|
||||
}
|
||||
|
||||
func (p *Captcha) Save() error {
|
||||
_, err := DB["rdb"].Insert(p)
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *Captcha) Del() error {
|
||||
_, err := DB["rdb"].Where("captcha_id=?", p.CaptchaId).Delete(new(Captcha))
|
||||
return err
|
||||
}
|
||||
|
||||
const captchaExpiresIn = 600
|
||||
|
||||
func (p Captcha) CleanUp() error {
|
||||
_, err := DB["rdb"].Exec("delete from captcha where created_at < ?", time.Now().Unix()-captchaExpiresIn)
|
||||
return err
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package cron
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/didi/nightingale/src/models"
|
||||
)
|
||||
|
||||
const cleanerInterval = 3600 * time.Second
|
||||
|
||||
func CleanerLoop() {
|
||||
tc := time.Tick(cleanerInterval)
|
||||
|
||||
for {
|
||||
models.AuthState{}.CleanUp()
|
||||
models.Captcha{}.CleanUp()
|
||||
<-tc
|
||||
}
|
||||
}
|
Loading…
Reference in new issue