80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
|
//这是使用官方例子改的, 试试能不能接上微信
|
||
|
package oauth2
|
||
|
|
||
|
import (
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"github.com/go-oauth2/oauth2/v4/generates"
|
||
|
"github.com/go-oauth2/oauth2/v4/manage"
|
||
|
"github.com/go-oauth2/oauth2/v4/models"
|
||
|
"github.com/go-oauth2/oauth2/v4/store"
|
||
|
"github.com/ycyxuehan/zelda/apiserver/auth/api"
|
||
|
)
|
||
|
|
||
|
type OAuth2 struct {
|
||
|
oauth2Manager *manage.Manager
|
||
|
clientStore *store.ClientStore
|
||
|
id string
|
||
|
secret string
|
||
|
domain string
|
||
|
}
|
||
|
|
||
|
func NewManager(id, secret, domain string) (api.AuthManager, error) {
|
||
|
o2 := &OAuth2{
|
||
|
id: id,
|
||
|
secret: secret,
|
||
|
domain: domain,
|
||
|
}
|
||
|
|
||
|
o2.createStore()
|
||
|
o2.createManager()
|
||
|
|
||
|
return o2, nil
|
||
|
}
|
||
|
|
||
|
func (o *OAuth2) createStore() {
|
||
|
clientStore := store.NewClientStore()
|
||
|
clientStore.Set(o.id, &models.Client{
|
||
|
ID: o.id,
|
||
|
Secret: o.secret,
|
||
|
Domain: o.domain,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func (o *OAuth2) createManager() {
|
||
|
o.oauth2Manager = manage.NewDefaultManager()
|
||
|
o.oauth2Manager.SetAuthorizeCodeTokenCfg(manage.DefaultAuthorizeCodeTokenCfg)
|
||
|
|
||
|
// token store
|
||
|
o.oauth2Manager.MustTokenStorage(store.NewMemoryTokenStore())
|
||
|
|
||
|
// generate jwt access token
|
||
|
// manager.MapAccessGenerate(generates.NewJWTAccessGenerate("", []byte("00000000"), jwt.SigningMethodHS512))
|
||
|
o.oauth2Manager.MapAccessGenerate(generates.NewAccessGenerate())
|
||
|
o.oauth2Manager.MapClientStorage(o.clientStore)
|
||
|
|
||
|
}
|
||
|
|
||
|
func (o *OAuth2) Login(f api.IdentifyFunc, data *api.AuthentitionRequest) *api.AuthentitionResponse {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (o *OAuth2) Logout(*api.AuthentitionRequest) *api.AuthentitionResponse {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (o *OAuth2) Refresh(*api.AuthentitionRequest) *api.AuthentitionResponse {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (o *OAuth2) KubernetesToken(*api.AuthentitionRequest) (string, error) {
|
||
|
return "", nil
|
||
|
}
|
||
|
|
||
|
func (o *OAuth2) MiddleWare() gin.HandlerFunc {
|
||
|
return func(c *gin.Context) {}
|
||
|
}
|
||
|
|
||
|
//初始化auth接口
|
||
|
func (o *OAuth2) InitAuthRoute(identifyFunc api.IdentifyFunc, authGroup *gin.RouterGroup) {
|
||
|
}
|