session.provider로 교체
This commit is contained in:
116
core/maingate.go
116
core/maingate.go
@ -22,6 +22,7 @@ import (
|
||||
"repositories.action2quare.com/ayo/gocommon"
|
||||
"repositories.action2quare.com/ayo/gocommon/flagx"
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
"repositories.action2quare.com/ayo/gocommon/session"
|
||||
|
||||
"github.com/golang-jwt/jwt"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
@ -73,55 +74,10 @@ func SessionTTL() time.Duration {
|
||||
return sessionTTL
|
||||
}
|
||||
|
||||
type mongoAuthCell struct {
|
||||
src *gocommon.Authinfo
|
||||
}
|
||||
|
||||
func (ac *mongoAuthCell) ToAuthinfo() *gocommon.Authinfo {
|
||||
if ac.src == nil {
|
||||
logger.Error("mongoAuthCell ToAuthinfo failed. ac.src is nil")
|
||||
}
|
||||
return ac.src
|
||||
}
|
||||
|
||||
func (ac *mongoAuthCell) ToBytes() []byte {
|
||||
bt, _ := json.Marshal(ac.src)
|
||||
return bt
|
||||
}
|
||||
|
||||
func makeAuthCollection(mongoClient gocommon.MongoClient, sessionTTL time.Duration) *gocommon.AuthCollection {
|
||||
authcoll := gocommon.MakeAuthCollection(sessionTTL)
|
||||
authcoll.SessionRemoved = func(sk string) {
|
||||
skid, _ := primitive.ObjectIDFromHex(sk)
|
||||
mongoClient.Delete(CollectionAuth, bson.M{
|
||||
"sk": skid,
|
||||
})
|
||||
}
|
||||
authcoll.QuerySession = func(sk string, token string) gocommon.AuthinfoCell {
|
||||
skid, _ := primitive.ObjectIDFromHex(sk)
|
||||
var outcell mongoAuthCell
|
||||
err := mongoClient.FindOneAs(CollectionAuth, bson.M{
|
||||
"sk": skid,
|
||||
}, &outcell.src, options.FindOne().SetHint("skonly"))
|
||||
|
||||
if err != nil {
|
||||
logger.Error("QuerySession failed :", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
if outcell.src == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &outcell
|
||||
}
|
||||
|
||||
return authcoll
|
||||
}
|
||||
|
||||
type maingateConfig struct {
|
||||
Mongo string `json:"maingate_mongodb_url"`
|
||||
SessionTTL int64 `json:"maingate_session_ttl"`
|
||||
SessionStorage string `json:"maingate_session_storage"`
|
||||
Autologin_ttl int64 `json:"autologin_ttl"`
|
||||
MaximumNumLinkAccount int64 `json:"maximum_num_link_account"`
|
||||
RedirectBaseUrl string `json:"redirect_base_url"`
|
||||
@ -166,7 +122,7 @@ type Maingate struct {
|
||||
|
||||
mongoClient gocommon.MongoClient
|
||||
|
||||
auths *gocommon.AuthCollection
|
||||
sessionProvider session.Provider
|
||||
//services servicelist
|
||||
serviceptr unsafe.Pointer
|
||||
admins unsafe.Pointer
|
||||
@ -387,7 +343,21 @@ func (mg *Maingate) prepare(context context.Context) (err error) {
|
||||
return makeErrorWithStack(err)
|
||||
}
|
||||
|
||||
mg.auths = makeAuthCollection(mg.mongoClient, time.Duration(mg.SessionTTL*int64(time.Second)))
|
||||
if len(mg.SessionStorage) > 0 {
|
||||
if strings.HasPrefix(mg.SessionStorage, "mongodb") {
|
||||
mg.sessionProvider, err = session.NewProviderWithMongo(context, mg.SessionStorage, "maingate", time.Duration(mg.SessionTTL*int64(time.Second)))
|
||||
} else if strings.HasPrefix(mg.SessionStorage, "redis") {
|
||||
mg.sessionProvider, err = session.NewProviderWithRedis(context, mg.SessionStorage, time.Duration(mg.SessionTTL*int64(time.Second)))
|
||||
} else {
|
||||
err = fmt.Errorf("sessio storage is not valid :%s", mg.SessionStorage)
|
||||
}
|
||||
} else {
|
||||
mg.sessionProvider, err = session.NewProviderWithMongo(context, mg.Mongo, "maingate", time.Duration(mg.SessionTTL*int64(time.Second)))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return makeErrorWithStack(err)
|
||||
}
|
||||
|
||||
var preall []struct {
|
||||
Link string `bson:"link"`
|
||||
@ -431,7 +401,6 @@ func (mg *Maingate) prepare(context context.Context) (err error) {
|
||||
}
|
||||
mg.bl.init(blocks)
|
||||
|
||||
go watchAuthCollection(context, mg.auths, mg.mongoClient)
|
||||
go mg.wl.watchCollection(context, CollectionWhitelist, mg.mongoClient)
|
||||
go mg.bl.watchCollection(context, CollectionBlock, mg.mongoClient)
|
||||
|
||||
@ -501,7 +470,6 @@ func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMu
|
||||
mg.service().serveHTTP(w, r)
|
||||
})
|
||||
serveMux.HandleFunc(gocommon.MakeHttpHandlerPattern(prefix, "api/"), mg.api)
|
||||
serveMux.HandleFunc(gocommon.MakeHttpHandlerPattern(prefix, "query/"), mg.query)
|
||||
|
||||
configraw, _ := json.Marshal(mg.maingateConfig)
|
||||
var convertedConfig map[string]any
|
||||
@ -582,54 +550,6 @@ func (mg *Maingate) RegisterHandlers(ctx context.Context, serveMux *http.ServeMu
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mg *Maingate) query(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
s := recover()
|
||||
if s != nil {
|
||||
logger.Error(s)
|
||||
}
|
||||
}()
|
||||
|
||||
defer func() {
|
||||
io.Copy(io.Discard, r.Body)
|
||||
r.Body.Close()
|
||||
}()
|
||||
|
||||
queryvals := r.URL.Query()
|
||||
sk := queryvals.Get("sk")
|
||||
|
||||
if len(sk) == 0 {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
info := mg.auths.Find(sk)
|
||||
if info == nil {
|
||||
logger.Println("session key is not valid :", sk)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
if !*devflag {
|
||||
apitoken := r.Header.Get("MG-X-API-TOKEN")
|
||||
if len(apitoken) == 0 {
|
||||
logger.Println("MG-X-API-TOKEN is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
apitokenObj, _ := primitive.ObjectIDFromHex(apitoken)
|
||||
if !mg.service().isValidToken(apitokenObj) {
|
||||
logger.Println("MG-X-API-TOKEN is invalid :", apitoken)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
bt, _ := json.Marshal(info)
|
||||
w.Write(bt)
|
||||
}
|
||||
|
||||
func (mg *Maingate) GeneratePlatformLoginNonceKey() string {
|
||||
const allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
b := make([]byte, 52)
|
||||
|
||||
Reference in New Issue
Block a user