Compare commits
25 Commits
5757a81cb8
...
kd-live
| Author | SHA1 | Date | |
|---|---|---|---|
| 2d060fe117 | |||
| 231b1a35d5 | |||
| 845784d204 | |||
| 424c8be420 | |||
| ce39e8d878 | |||
| 54c727117b | |||
| 7816fd56c0 | |||
| 564a2ee14a | |||
| cbcae00c77 | |||
| 28dc84769d | |||
| 0c678947cd | |||
| dbd0a7bf5f | |||
| 86247e053d | |||
| 757c6d312c | |||
| e37a974d9c | |||
| 470591cb44 | |||
| 38114769b3 | |||
| b05473a1c6 | |||
| 0fff694e8a | |||
| d8713298c4 | |||
| 763b0fc4bd | |||
| d8e18d7ffc | |||
| 381f1edb80 | |||
| a8e448b8ca | |||
| 9b0e3a5d5b |
@ -1,6 +1,7 @@
|
||||
{
|
||||
"maingate_mongodb_url": "mongodb://...",
|
||||
"autologin_ttl": 604800,
|
||||
"acc_del_ttl": 7776000,
|
||||
"maximum_num_link_account": 10,
|
||||
"redirect_base_url": "",
|
||||
"google_client_id" : "",
|
||||
|
||||
92
core/api.go
92
core/api.go
@ -161,14 +161,7 @@ func (caller apiCaller) uploadAPI(w http.ResponseWriter, r *http.Request) error
|
||||
func (caller apiCaller) blockAPI(w http.ResponseWriter, r *http.Request) error {
|
||||
mg := caller.mg
|
||||
if r.Method == "GET" {
|
||||
target, ok := gocommon.ReadObjectIDFormValue(r.Form, "accid")
|
||||
if ok {
|
||||
json.NewEncoder(w).Encode(mg.bl.all())
|
||||
} else if !target.IsZero() {
|
||||
if blocked, ok := mg.bl.get(target); ok && blocked != nil {
|
||||
json.NewEncoder(w).Encode(blocked)
|
||||
}
|
||||
}
|
||||
json.NewEncoder(w).Encode(mg.bl.all())
|
||||
} else if r.Method == "PUT" {
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
|
||||
@ -379,6 +372,11 @@ func (caller apiCaller) couponAPI(w http.ResponseWriter, r *http.Request) error
|
||||
logger.Println("begin listAllCouponNames")
|
||||
listAllCouponNames(caller.mg.mongoClient, w, r)
|
||||
}
|
||||
|
||||
case "DELETE":
|
||||
// 쿠폰 삭제
|
||||
logger.Println("begin deleteCoupon")
|
||||
deleteCoupon(caller.mg.mongoClient, w, r)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -461,34 +459,52 @@ func (mg *Maingate) api(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var userinfo map[string]any
|
||||
|
||||
var apiTokenObj primitive.ObjectID
|
||||
if !*devflag {
|
||||
authheader := r.Header.Get("Authorization")
|
||||
if len(authheader) == 0 {
|
||||
logger.Println("Authorization header is not valid :", authheader)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
apiToken := r.Header.Get("MG-X-API-TOKEN")
|
||||
if len(apiToken) > 0 {
|
||||
if apiToken != mg.maingateConfig.ApiToken {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
req, _ := http.NewRequest("GET", "https://graph.microsoft.com/oidc/userinfo", nil)
|
||||
req.Header.Add("Authorization", authheader)
|
||||
client := &http.Client{}
|
||||
obj, err := primitive.ObjectIDFromHex(apiToken)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
logger.Println("graph microsoft api call failed :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
apiTokenObj = obj
|
||||
} else {
|
||||
authheader := r.Header.Get("Authorization")
|
||||
if len(authheader) == 0 {
|
||||
logger.Println("Authorization header is not valid :", authheader)
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
if err = json.Unmarshal(raw, &userinfo); err != nil {
|
||||
return
|
||||
}
|
||||
req, _ := http.NewRequest("GET", "https://graph.microsoft.com/oidc/userinfo", nil)
|
||||
req.Header.Add("Authorization", authheader)
|
||||
client := &http.Client{}
|
||||
|
||||
if _, expired := userinfo["error"]; expired {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
logger.Println("graph microsoft api call failed :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
if err = json.Unmarshal(raw, &userinfo); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if _, expired := userinfo["error"]; expired {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -504,20 +520,6 @@ func (mg *Maingate) api(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
var apiTokenObj primitive.ObjectID
|
||||
if !*devflag {
|
||||
apiToken := r.Header.Get("MG-X-API-TOKEN")
|
||||
if len(apiToken) > 0 {
|
||||
obj, err := primitive.ObjectIDFromHex(apiToken)
|
||||
if err != nil {
|
||||
logger.Error(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
apiTokenObj = obj
|
||||
}
|
||||
}
|
||||
|
||||
logger.Println("api call :", r.URL.Path, r.Method, r.URL.Query(), userinfo)
|
||||
caller := apiCaller{
|
||||
userinfo: userinfo,
|
||||
|
||||
@ -30,6 +30,7 @@ type couponDoc struct {
|
||||
Total int64 `json:"total" bson:"total"`
|
||||
Remains []string `json:"remains,omitempty" bson:"remains,omitempty"`
|
||||
Used []string `json:"used,omitempty" bson:"used,omitempty"`
|
||||
Expire int64 `json:"expire" bson:"expire"`
|
||||
}
|
||||
|
||||
func makeCouponKey(roundnum uint32, uid []byte) string {
|
||||
@ -72,6 +73,7 @@ func generateCoupons(mongoClient gocommon.MongoClient, w http.ResponseWriter, r
|
||||
effect, _ := gocommon.ReadStringFormValue(r.Form, "effect")
|
||||
count, _ := gocommon.ReadIntegerFormValue(r.Form, "count")
|
||||
desc, _ := gocommon.ReadStringFormValue(r.Form, "desc")
|
||||
expire, _ := gocommon.ReadIntegerFormValue(r.Form, "expire")
|
||||
|
||||
if count == 0 {
|
||||
logger.Println("[generateCoupons] count == 0")
|
||||
@ -92,6 +94,7 @@ func generateCoupons(mongoClient gocommon.MongoClient, w http.ResponseWriter, r
|
||||
Effect: effect,
|
||||
Desc: desc,
|
||||
Total: -1,
|
||||
Expire: expire,
|
||||
},
|
||||
}, options.Update().SetUpsert(true)); err != nil {
|
||||
logger.Println("[generateCoupons] Update failed :", err)
|
||||
@ -154,6 +157,7 @@ func generateCoupons(mongoClient gocommon.MongoClient, w http.ResponseWriter, r
|
||||
Effect: effect,
|
||||
Desc: desc,
|
||||
Total: count,
|
||||
Expire: expire,
|
||||
},
|
||||
}, options.Update().SetUpsert(true))
|
||||
}
|
||||
@ -198,7 +202,12 @@ func downloadCoupons(mongoClient gocommon.MongoClient, w http.ResponseWriter, r
|
||||
roundnum := binary.BigEndian.Uint32(roundObj[:])
|
||||
var coupons []string
|
||||
for _, uid := range coupon.Remains {
|
||||
coupons = append(coupons, makeCouponKey(roundnum, []byte(uid)))
|
||||
decUid, err := hex.DecodeString(uid)
|
||||
if err != nil {
|
||||
logger.Println("downloadCoupons Fail", err)
|
||||
continue
|
||||
}
|
||||
coupons = append(coupons, makeCouponKey(roundnum, decUid))
|
||||
}
|
||||
|
||||
enc := json.NewEncoder(w)
|
||||
@ -231,7 +240,7 @@ func queryCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *htt
|
||||
var coupon couponDoc
|
||||
if err := mongoClient.FindOneAs(CollectionCoupon, bson.M{
|
||||
"_id": roundObj,
|
||||
}, &coupon, options.FindOne().SetProjection(bson.M{"effect": 1, "name": 1, "reason": 1, "total": 1, "desc": 1}).SetReturnKey(false)); err != nil {
|
||||
}, &coupon, options.FindOne().SetProjection(bson.M{"effect": 1, "name": 1, "reason": 1, "total": 1, "desc": 1, "expire": 1}).SetReturnKey(false)); err != nil {
|
||||
logger.Println("[queryCoupon] FindOneAs failed :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
@ -275,8 +284,8 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http.
|
||||
round, _ = coupon.MakeCouponRoundHash(code)
|
||||
}
|
||||
|
||||
// 1. 내가 이 라운드의 쿠폰을 쓴 적이 있나
|
||||
already, err := mongoClient.Exists(CollectionCouponUse, bson.M{
|
||||
// 쿠폰 사용 유무 검사
|
||||
alreadyused, err := mongoClient.Exists(CollectionCouponUse, bson.M{
|
||||
"_id": acc,
|
||||
"rounds": round,
|
||||
})
|
||||
@ -286,7 +295,7 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http.
|
||||
return
|
||||
}
|
||||
|
||||
if already {
|
||||
if alreadyused {
|
||||
// 이미 이 라운드의 쿠폰을 사용한 적이 있다.
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
return
|
||||
@ -298,7 +307,7 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http.
|
||||
// 무한 쿠폰일 수 있으므로 존재하는지 확인
|
||||
if err := mongoClient.FindOneAs(CollectionCoupon, bson.M{
|
||||
"_id": roundObj,
|
||||
}, &coupon, options.FindOne().SetProjection(bson.M{"_id": 0, "effect": 1, "name": 1, "reason": 1, "total": 1})); err != nil {
|
||||
}, &coupon, options.FindOne().SetProjection(bson.M{"_id": 0, "effect": 1, "name": 1, "total": 1, "expire": 1})); err != nil {
|
||||
logger.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
@ -310,9 +319,10 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http.
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// 2. 쿠폰을 하나 꺼냄
|
||||
// 쿠폰을 하나 꺼냄
|
||||
matched, _, err := mongoClient.Update(CollectionCoupon, bson.M{
|
||||
"_id": roundObj,
|
||||
"_id": roundObj,
|
||||
"remains": key,
|
||||
}, bson.M{
|
||||
"$pull": bson.M{"remains": key},
|
||||
})
|
||||
@ -328,18 +338,24 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http.
|
||||
return
|
||||
}
|
||||
|
||||
// 3. round의 효과 읽기
|
||||
// round의 효과 읽기
|
||||
if err := mongoClient.FindOneAndUpdateAs(CollectionCoupon, bson.M{
|
||||
"_id": roundObj,
|
||||
}, bson.M{
|
||||
"$push": bson.M{"used": key},
|
||||
}, &coupon, options.FindOneAndUpdate().SetProjection(bson.M{"effect": 1})); err != nil {
|
||||
}, &coupon, options.FindOneAndUpdate().SetProjection(bson.M{"effect": 1, "expire": 1})); err != nil {
|
||||
logger.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if coupon.Expire < time.Now().Unix() {
|
||||
// 쿠폰 만료시간 경과
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if len(coupon.Effect) == 0 {
|
||||
// 쿠폰이 없네?
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
@ -370,3 +386,22 @@ func useCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http.
|
||||
|
||||
w.Write([]byte(coupon.Effect))
|
||||
}
|
||||
|
||||
func deleteCoupon(mongoClient gocommon.MongoClient, w http.ResponseWriter, r *http.Request) {
|
||||
code, _ := gocommon.ReadStringFormValue(r.Form, "name")
|
||||
if len(code) == 0 {
|
||||
logger.Println("coupon delete code error")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
_, err := mongoClient.Delete(CollectionCoupon, bson.M{
|
||||
"name": code,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
logger.Println("coupon delete error")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@ -123,7 +123,9 @@ func makeAuthCollection(mongoClient gocommon.MongoClient, sessionTTL time.Durati
|
||||
type maingateConfig struct {
|
||||
Mongo string `json:"maingate_mongodb_url"`
|
||||
SessionTTL int64 `json:"maingate_session_ttl"`
|
||||
ApiToken string `json:"maingate_api_token"`
|
||||
Autologin_ttl int64 `json:"autologin_ttl"`
|
||||
AccDelTTL int64 `json:"acc_del_ttl"`
|
||||
MaximumNumLinkAccount int64 `json:"maximum_num_link_account"`
|
||||
RedirectBaseUrl string `json:"redirect_base_url"`
|
||||
GoogleClientId string `json:"google_client_id"`
|
||||
@ -354,6 +356,14 @@ func (mg *Maingate) prepare(context context.Context) (err error) {
|
||||
return makeErrorWithStack(err)
|
||||
}
|
||||
|
||||
if err = mg.mongoClient.MakeExpireIndex(CollectionAccount, int32(mg.AccDelTTL)); err != nil {
|
||||
return makeErrorWithStack(err)
|
||||
}
|
||||
|
||||
if err = mg.mongoClient.MakeExpireIndex(CollectionLink, int32(mg.AccDelTTL)); err != nil {
|
||||
return makeErrorWithStack(err)
|
||||
}
|
||||
|
||||
// Delete대신 _ts로 expire시킴. pipeline에 삭제 알려주기 위함
|
||||
if err = mg.mongoClient.MakeExpireIndex(CollectionWhitelist, 10); err != nil {
|
||||
return makeErrorWithStack(err)
|
||||
|
||||
@ -95,7 +95,7 @@ func (p *memberContainerPtr[K, T]) contains(key K, out *T) bool {
|
||||
return false
|
||||
}
|
||||
if out != nil {
|
||||
out = &found
|
||||
*out = found
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
191
core/service.go
191
core/service.go
@ -322,7 +322,7 @@ func (sh *serviceDescription) link(w http.ResponseWriter, r *http.Request) {
|
||||
if !guestlink {
|
||||
_, err = sh.readProfile(oldType, oldId, bfinfo)
|
||||
if err != nil {
|
||||
logger.Error("readProfile(old) failed :", err)
|
||||
logger.Println("readProfile(old) failed :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -332,7 +332,7 @@ func (sh *serviceDescription) link(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
oldType, oldId, err = sh.getProviderInfo(oldType, oldId)
|
||||
if err != nil {
|
||||
logger.Error("getProviderInfo failed :", err)
|
||||
logger.Println("getProviderInfo failed :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -346,7 +346,7 @@ func (sh *serviceDescription) link(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
email, err := sh.readProfile(newType, newId, bfinfo)
|
||||
if err != nil {
|
||||
logger.Error("readProfile(new) failed :", err)
|
||||
logger.Println("readProfile(new) failed :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -359,7 +359,7 @@ func (sh *serviceDescription) link(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
newType, newId, err = sh.getProviderInfo(newType, newId)
|
||||
if err != nil {
|
||||
logger.Error("getProviderInfo failed :", err)
|
||||
logger.Println("getProviderInfo failed :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -390,7 +390,7 @@ func (sh *serviceDescription) link(w http.ResponseWriter, r *http.Request) {
|
||||
}, options.Update().SetUpsert(true))
|
||||
if err != nil {
|
||||
logger.Error("link failed. Update ServiceName err :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@ -457,7 +457,7 @@ func (sh *serviceDescription) unlink(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
sType, sId, err := sh.getProviderInfo(sType, sId)
|
||||
if err != nil {
|
||||
logger.Error("getProviderInfo failed :", err)
|
||||
logger.Println("getProviderInfo failed :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -476,7 +476,7 @@ func (sh *serviceDescription) unlink(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if err != nil {
|
||||
logger.Error("unlink failed, fail to count accounts :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
if len(accDocs) <= 1 {
|
||||
@ -505,7 +505,7 @@ func (sh *serviceDescription) unlink(w http.ResponseWriter, r *http.Request) {
|
||||
}, options.FindOneAndDelete().SetProjection(bson.M{"_id": 1}))
|
||||
if err != nil {
|
||||
logger.Error("unlink failed. Delete ServiceName err :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if preid == nil {
|
||||
@ -554,7 +554,7 @@ func (sh *serviceDescription) linkinfo(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
sType, sId, err := sh.getProviderInfo(sType, sId)
|
||||
if err != nil {
|
||||
logger.Error("getProviderInfo failed :", err)
|
||||
logger.Println("getProviderInfo failed :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
@ -574,7 +574,7 @@ func (sh *serviceDescription) linkinfo(w http.ResponseWriter, r *http.Request) {
|
||||
}))
|
||||
if err != nil {
|
||||
logger.Error("linkinfo failed. CountDocuments err :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@ -588,6 +588,11 @@ func (sh *serviceDescription) linkinfo(w http.ResponseWriter, r *http.Request) {
|
||||
}, options.Find().SetLimit(sh.MaximumNumLinkAccount).SetProjection(bson.M{
|
||||
platformName: 1,
|
||||
}))
|
||||
if err != nil {
|
||||
logger.Error("linkinfo failed. FindAll returns err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var linkstrs []string
|
||||
for _, link := range links {
|
||||
@ -597,7 +602,7 @@ func (sh *serviceDescription) linkinfo(w http.ResponseWriter, r *http.Request) {
|
||||
linkbytes, err := json.Marshal(linkstrs)
|
||||
if err != nil {
|
||||
logger.Error("linkinfo failed. json marshal fail :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
@ -605,6 +610,49 @@ func (sh *serviceDescription) linkinfo(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write(linkbytes)
|
||||
}
|
||||
|
||||
// == 계정 이메일 조회
|
||||
func (sh *serviceDescription) emailinfo(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
s := recover()
|
||||
if s != nil {
|
||||
logger.Error(s)
|
||||
}
|
||||
}()
|
||||
|
||||
if r.Method != "GET" {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
queryvals := r.URL.Query()
|
||||
sk := queryvals.Get("sk")
|
||||
|
||||
authInfo := sh.auths.Find(sk)
|
||||
if authInfo == nil {
|
||||
logger.Println(" session key is not valid :", sk)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
email := authInfo.Email
|
||||
|
||||
if strings.HasPrefix(email, "__dummy_") && strings.HasSuffix(email, "temp__") {
|
||||
email = ""
|
||||
}
|
||||
|
||||
if strings.HasSuffix(email, "@noauth.flag") || strings.HasSuffix(email, "@guest.flag") {
|
||||
email = ""
|
||||
}
|
||||
|
||||
// fmt.Println("=================")
|
||||
// fmt.Println(email)
|
||||
// fmt.Println("=================")
|
||||
//logger.Println("Email :", email)
|
||||
|
||||
w.Write([]byte(fmt.Sprintf(`{"email":"%s"}`, email)))
|
||||
|
||||
}
|
||||
|
||||
func (sh *serviceDescription) authorize(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
s := recover()
|
||||
@ -630,29 +678,29 @@ func (sh *serviceDescription) authorize(w http.ResponseWriter, r *http.Request)
|
||||
//email, err := sh.readProfile(authtype, uid, accesstoken)
|
||||
bfinfo, err := sh.getUserBrowserInfo(r)
|
||||
if err != nil {
|
||||
logger.Error("getUserBrowserInfo failed :", err)
|
||||
logger.Println("getUserBrowserInfo failed :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
email, err = sh.readProfile(authtype, uid, bfinfo)
|
||||
if err != nil {
|
||||
logger.Error("readProfile failed :", err)
|
||||
logger.Println("readProfile failed :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
newType, newId, err := sh.getProviderInfo(authtype, uid)
|
||||
if err != nil {
|
||||
logger.Error("getProviderInfo failed :", err)
|
||||
logger.Println("getProviderInfo failed :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if authtype != newType || uid != newId {
|
||||
logger.Printf("auth success ( redirect ) : %s->%s, %s->%s, %s, %s", authtype, newType, uid, newId, email, session)
|
||||
authtype = newType
|
||||
uid = newId
|
||||
logger.Println("auth success ( redirect ) :", authtype, uid, email, session)
|
||||
}
|
||||
} else {
|
||||
email = fmt.Sprintf("%s@guest.flag", uid)
|
||||
@ -673,13 +721,15 @@ func (sh *serviceDescription) authorize(w http.ResponseWriter, r *http.Request)
|
||||
"create": createtime,
|
||||
"email": email,
|
||||
},
|
||||
}, options.FindOneAndUpdate().SetReturnDocument(options.After).SetUpsert(true).SetProjection(bson.M{"_id": 1}))
|
||||
}, options.FindOneAndUpdate().SetReturnDocument(options.After).SetUpsert(true).SetProjection(bson.M{
|
||||
"_id": 1,
|
||||
"_ts": 1,
|
||||
}))
|
||||
if err != nil {
|
||||
logger.Error("authorize failed :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
linkid := link["_id"].(primitive.ObjectID)
|
||||
newaccid := primitive.NewObjectID()
|
||||
for i := 0; i < len(sh.serviceCodeBytes); i++ {
|
||||
@ -733,6 +783,8 @@ func (sh *serviceDescription) authorize(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
logger.Println("session created :", accid, authtype, uid, email, newsession)
|
||||
|
||||
output := map[string]any{
|
||||
"sk": newsession.Hex(),
|
||||
"expirein": sh.sessionTTL.Seconds(),
|
||||
@ -742,6 +794,14 @@ func (sh *serviceDescription) authorize(w http.ResponseWriter, r *http.Request)
|
||||
if *noauth {
|
||||
output["noauth"] = true
|
||||
}
|
||||
|
||||
if link["_ts"] != nil {
|
||||
delts := link["_ts"].(primitive.DateTime)
|
||||
if !delts.Time().IsZero() {
|
||||
// 삭제된 계정. 삭제 되었다고 알려주자
|
||||
w.Header().Add("MG-ACCOUNT-DELETED", "TRUE")
|
||||
}
|
||||
}
|
||||
bt, _ := json.Marshal(output)
|
||||
w.Write(bt)
|
||||
} else if len(session) > 0 {
|
||||
@ -757,8 +817,7 @@ func (sh *serviceDescription) authorize(w http.ResponseWriter, r *http.Request)
|
||||
},
|
||||
}, options.Update().SetUpsert(false))
|
||||
if err != nil {
|
||||
logger.Error("update auth collection failed")
|
||||
logger.Error(err)
|
||||
logger.Error("update auth collection failed :", err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -773,6 +832,31 @@ func (sh *serviceDescription) authorize(w http.ResponseWriter, r *http.Request)
|
||||
"sk": session,
|
||||
"expirein": sh.sessionTTL.Seconds(),
|
||||
}
|
||||
|
||||
logger.Println("session updated :", authtype, uid, session)
|
||||
|
||||
authInfo := sh.auths.Find(session)
|
||||
if authInfo == nil {
|
||||
// 잘못된 세션
|
||||
logger.Println("authorize failed. fail to find authInfo :", session)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
//혹시 삭제 된 계정 아닌지 확인해본다.
|
||||
link, err := sh.mongoClient.FindOne(CollectionLink, bson.M{
|
||||
"platform": authtype,
|
||||
"uid": uid,
|
||||
}, options.FindOne().SetProjection(bson.M{
|
||||
"_ts": 1,
|
||||
}))
|
||||
if link["_ts"] != nil {
|
||||
delts := link["_ts"].(primitive.DateTime)
|
||||
if !delts.Time().IsZero() {
|
||||
// 삭제된 계정. 삭제 되었다고 알려주자
|
||||
w.Header().Add("MG-ACCOUNT-DELETED", "TRUE")
|
||||
}
|
||||
}
|
||||
bt, _ := json.Marshal(output)
|
||||
w.Write(bt)
|
||||
} else {
|
||||
@ -815,6 +899,7 @@ func (sh *serviceDescription) delacc(w http.ResponseWriter, r *http.Request) {
|
||||
sType := queryvals.Get("stype")
|
||||
sId := queryvals.Get("sid")
|
||||
sk := queryvals.Get("sk")
|
||||
cancel := queryvals.Has("cancel")
|
||||
|
||||
authInfo := sh.auths.Find(sk)
|
||||
if authInfo == nil {
|
||||
@ -824,13 +909,21 @@ func (sh *serviceDescription) delacc(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
originAuthType := sType
|
||||
sType, sId, err := sh.getProviderInfo(sType, sId)
|
||||
if err != nil {
|
||||
logger.Error("delacc failed. getProviderInfo err :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if authInfo.Uid != sId || authInfo.Platform != sType {
|
||||
logger.Println("delacc failed. session key is not correct :", *authInfo, queryvals)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
accids, err := sh.mongoClient.FindAll(CollectionAccount, bson.M{
|
||||
linkidMap, err := sh.mongoClient.FindAll(CollectionAccount, bson.M{
|
||||
"accid": authInfo.Accid,
|
||||
}, options.Find().SetProjection(bson.M{
|
||||
"_id": 1,
|
||||
@ -841,27 +934,55 @@ func (sh *serviceDescription) delacc(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var addIdFilter bson.A
|
||||
for _, accid := range accids {
|
||||
addIdFilter = append(addIdFilter, accid["_id"].(primitive.ObjectID))
|
||||
var linkidAry primitive.A
|
||||
for _, linkid := range linkidMap {
|
||||
linkidAry = append(linkidAry, linkid["_id"].(primitive.ObjectID))
|
||||
}
|
||||
|
||||
delfilter := bson.D{{Key: "_id", Value: bson.D{{Key: "$in", Value: addIdFilter}}}}
|
||||
delaccnum, err := sh.mongoClient.DeleteMany(CollectionAccount, delfilter)
|
||||
if err != nil {
|
||||
logger.Error("delacc failed. Delete many CollectionAccount err :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
delfilter := primitive.M{"_id": bson.M{"$in": linkidAry}}
|
||||
var delop primitive.M
|
||||
if !cancel {
|
||||
curtime := primitive.NewDateTimeFromTime(time.Now().UTC())
|
||||
delop = primitive.M{
|
||||
"$set": primitive.M{"_ts": curtime},
|
||||
}
|
||||
|
||||
if originAuthType == AuthPlatformFirebaseAuth {
|
||||
sh.mongoClient.Delete(CollectionFirebaseUserInfo, bson.M{
|
||||
"firebaseuserid": sId,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
delfilter["platform"] = sType
|
||||
targetLinkId, err := sh.mongoClient.FindAll(CollectionLink, delfilter, options.Find().SetProjection(bson.M{
|
||||
"_id": 1,
|
||||
}))
|
||||
if len(targetLinkId) != 1 {
|
||||
logger.Error("delacc failed. FindAll link err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
delfilter = primitive.M{"_id": targetLinkId[0]["_id"].(primitive.ObjectID)}
|
||||
delop = primitive.M{
|
||||
"$unset": primitive.M{"_ts": true},
|
||||
}
|
||||
}
|
||||
updated, _, err := sh.mongoClient.Update(CollectionAccount, delfilter, delop, options.Update().SetUpsert(false))
|
||||
if !updated || err != nil {
|
||||
logger.Error("delacc failed. Update CollectionAccount timestamp err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = sh.mongoClient.DeleteMany(CollectionLink, delfilter)
|
||||
if err != nil {
|
||||
logger.Error("delacc failed. Delete many CollectionLink err :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
updated, _, err = sh.mongoClient.Update(CollectionLink, delfilter, delop, options.Update().SetUpsert(false))
|
||||
if !updated || err != nil {
|
||||
logger.Error("delacc failed. Update CollectionLink timestamp err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
logger.Println("delacc success :", delaccnum)
|
||||
logger.Println("delacc success :", linkidMap)
|
||||
}
|
||||
|
||||
func (sh *serviceDescription) serveHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
@ -885,6 +1006,8 @@ func (sh *serviceDescription) serveHTTP(w http.ResponseWriter, r *http.Request)
|
||||
sh.unlink(w, r)
|
||||
} else if strings.HasSuffix(r.URL.Path, "/linkinfo") {
|
||||
sh.linkinfo(w, r)
|
||||
} else if strings.HasSuffix(r.URL.Path, "/emailinfo") {
|
||||
sh.emailinfo(w, r)
|
||||
} else if strings.HasSuffix(r.URL.Path, "/delacc") {
|
||||
sh.delacc(w, r)
|
||||
} else if strings.HasSuffix(r.URL.Path, "/divs") {
|
||||
@ -964,7 +1087,7 @@ func (sh *serviceDescription) serveHTTP(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
logger.Println("div is not found :", divname)
|
||||
logger.Println("div is not found :", divname, sh.Divisions)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
} else {
|
||||
|
||||
2
go.mod
2
go.mod
@ -7,7 +7,7 @@ require (
|
||||
github.com/golang-jwt/jwt v3.2.2+incompatible
|
||||
go.mongodb.org/mongo-driver v1.11.7
|
||||
google.golang.org/api v0.128.0
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230912075917-f9a146321cdb
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20240205060841-c31f838ba8a9
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
8
go.sum
8
go.sum
@ -268,11 +268,7 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230710085810-8173216e9574 h1:Ha0d/sv/MzC3ASCTXfe2tAFJieLNJmTCBL8aETEOY14=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230710085810-8173216e9574/go.mod h1:rn6NA28Mej+qgLNx/Bu2wsdGyIycmacqlNP6gUXX2a0=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230911034515-1af5d7281946 h1:YSvgTNuHeKis37+FfOvzVLYCaXQ0oF+CWBTy4bRqq3g=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230911034515-1af5d7281946/go.mod h1:XvklTTSvQX5uviivGBcZo8eIL+mV94W2e4uBBXcT5JY=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230912074842-ea8ae4d02c7d h1:W6Ir1+kA6wMDya7bYz2S2X96qhb25FKpa/+wnHnSNVo=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230912074842-ea8ae4d02c7d/go.mod h1:rn6NA28Mej+qgLNx/Bu2wsdGyIycmacqlNP6gUXX2a0=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230912075917-f9a146321cdb h1:Rdf6uhBIWunRLZ2LIT1hSovYXxZoOzx9mdSK5bjWpos=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230912075917-f9a146321cdb/go.mod h1:rn6NA28Mej+qgLNx/Bu2wsdGyIycmacqlNP6gUXX2a0=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20240205060841-c31f838ba8a9 h1:5cQ60XjlI7k0qld0rIpd6gy7+a9csv3ijz1EVKTzsy8=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20240205060841-c31f838ba8a9/go.mod h1:rn6NA28Mej+qgLNx/Bu2wsdGyIycmacqlNP6gUXX2a0=
|
||||
|
||||
Reference in New Issue
Block a user