firebase 설정에 따라 인스턴스 생성
This commit is contained in:
@ -36,6 +36,7 @@ import (
|
|||||||
|
|
||||||
var devflag = flagx.Bool("dev", false, "")
|
var devflag = flagx.Bool("dev", false, "")
|
||||||
var noauth = flagx.Bool("noauth", false, "")
|
var noauth = flagx.Bool("noauth", false, "")
|
||||||
|
var authtype = flagx.String("auth", "", "yes|no|both")
|
||||||
|
|
||||||
var (
|
var (
|
||||||
CollectionLink = gocommon.CollectionName("link")
|
CollectionLink = gocommon.CollectionName("link")
|
||||||
@ -133,6 +134,11 @@ func (ga *globalAdmins) parse() {
|
|||||||
ga.modtime = gocommon.ConfigModTime()
|
ga.modtime = gocommon.ConfigModTime()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type firebaseClient struct {
|
||||||
|
firebaseAppClient *auth.Client
|
||||||
|
firebaseAppContext context.Context
|
||||||
|
}
|
||||||
|
|
||||||
// Maingate :
|
// Maingate :
|
||||||
type Maingate struct {
|
type Maingate struct {
|
||||||
mongoClient gocommon.MongoClient
|
mongoClient gocommon.MongoClient
|
||||||
@ -148,8 +154,8 @@ type Maingate struct {
|
|||||||
authorizationEndpoints map[string]string
|
authorizationEndpoints map[string]string
|
||||||
userinfoEndpoint map[string]string
|
userinfoEndpoint map[string]string
|
||||||
jwksUri map[string]string
|
jwksUri map[string]string
|
||||||
firebaseAppClient *auth.Client
|
|
||||||
firebaseAppContext context.Context
|
firebase *firebaseClient
|
||||||
}
|
}
|
||||||
|
|
||||||
var config maingateConfig
|
var config maingateConfig
|
||||||
@ -186,18 +192,29 @@ func New(ctx context.Context) (*Maingate, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !*noauth {
|
if *noauth && len(*authtype) == 0 {
|
||||||
opt := option.WithCredentialsFile(config.FirebaseAdminSDKCredentialFile)
|
*authtype = "no"
|
||||||
firebaseApp, err := firebase.NewApp(context.Background(), nil, opt)
|
}
|
||||||
if err != nil {
|
|
||||||
logger.Error("firebase admin error initializing app failed :", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
mg.firebaseAppContext = ctx
|
if *authtype == "yes" || *authtype == "both" {
|
||||||
mg.firebaseAppClient, err = firebaseApp.Auth(mg.firebaseAppContext)
|
if len(config.FirebaseAdminSDKCredentialFile) > 0 {
|
||||||
if err != nil {
|
opt := option.WithCredentialsFile(config.FirebaseAdminSDKCredentialFile)
|
||||||
logger.Println("FirebaseAppClient error getting Auth client:", err)
|
firebaseApp, err := firebase.NewApp(context.Background(), nil, opt)
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("firebase admin error initializing app failed :", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
firebaseAppClient, err := firebaseApp.Auth(ctx)
|
||||||
|
if err != nil {
|
||||||
|
logger.Println("FirebaseAppClient error getting Auth client:", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
mg.firebase = &firebaseClient{
|
||||||
|
firebaseAppContext: ctx,
|
||||||
|
firebaseAppClient: firebaseAppClient,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -147,6 +147,11 @@ func (mg *Maingate) platform_firebaseauth_authorize_sdk(w http.ResponseWriter, r
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (mg *Maingate) platform_firebaseauth_authorize_raw(w http.ResponseWriter, brinfo, code, state, cookieSessionKey, memberId, nickname, provider, providerId, email, photourl, phonenumber string) (bool, string) {
|
func (mg *Maingate) platform_firebaseauth_authorize_raw(w http.ResponseWriter, brinfo, code, state, cookieSessionKey, memberId, nickname, provider, providerId, email, photourl, phonenumber string) (bool, string) {
|
||||||
|
if mg.firebase == nil {
|
||||||
|
logger.Println("mg.firebase is nil. check 'firebase_admin_sdk_credentialfile' config or 'authtype' parameter")
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
|
||||||
found, err := mg.mongoClient.FindOne(CollectionPlatformLoginToken, bson.M{
|
found, err := mg.mongoClient.FindOne(CollectionPlatformLoginToken, bson.M{
|
||||||
"platform": AuthPlatformFirebaseAuth,
|
"platform": AuthPlatformFirebaseAuth,
|
||||||
@ -189,7 +194,7 @@ func (mg *Maingate) platform_firebaseauth_authorize_raw(w http.ResponseWriter, b
|
|||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = mg.firebaseAppClient.VerifyIDToken(mg.firebaseAppContext, code)
|
_, err = mg.firebase.firebaseAppClient.VerifyIDToken(mg.firebase.firebaseAppContext, code)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error verifying ID token:", err)
|
log.Println("error verifying ID token:", err)
|
||||||
return false, ""
|
return false, ""
|
||||||
@ -243,6 +248,10 @@ func (mg *Maingate) platform_firebaseauth_authorize_raw(w http.ResponseWriter, b
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (mg *Maingate) platform_firebase_getuserinfo(info usertokeninfo) (bool, string, string) {
|
func (mg *Maingate) platform_firebase_getuserinfo(info usertokeninfo) (bool, string, string) {
|
||||||
|
if mg.firebase == nil {
|
||||||
|
logger.Println("mg.firebase is nil. check 'firebase_admin_sdk_credentialfile' config or 'authtype' parameter")
|
||||||
|
return false, "", ""
|
||||||
|
}
|
||||||
|
|
||||||
found, err := mg.mongoClient.FindOne(CollectionFirebaseUserInfo, bson.M{
|
found, err := mg.mongoClient.FindOne(CollectionFirebaseUserInfo, bson.M{
|
||||||
"firebaseuserid": info.userid,
|
"firebaseuserid": info.userid,
|
||||||
@ -257,7 +266,7 @@ func (mg *Maingate) platform_firebase_getuserinfo(info usertokeninfo) (bool, str
|
|||||||
return false, "", ""
|
return false, "", ""
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = mg.firebaseAppClient.VerifyIDToken(mg.firebaseAppContext, info.token)
|
_, err = mg.firebase.firebaseAppClient.VerifyIDToken(mg.firebase.firebaseAppContext, info.token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error verifying ID token:", err)
|
log.Println("error verifying ID token:", err)
|
||||||
return false, "", ""
|
return false, "", ""
|
||||||
|
|||||||
Reference in New Issue
Block a user