From 8cda1aa4c7ef2f032f488ddff608bf15afebc5d8 Mon Sep 17 00:00:00 2001 From: mountain Date: Sun, 21 Jul 2024 22:24:58 +0900 Subject: [PATCH] =?UTF-8?q?http=20api=20method=20=EB=AA=A9=EB=A1=9D=20?= =?UTF-8?q?=EB=A6=AC=ED=84=B4=ED=95=98=EB=8A=94=20=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apicaller/api_caller_auths.go | 216 ---------------------------------- server.go | 8 ++ 2 files changed, 8 insertions(+), 216 deletions(-) delete mode 100644 apicaller/api_caller_auths.go diff --git a/apicaller/api_caller_auths.go b/apicaller/api_caller_auths.go deleted file mode 100644 index 085d075..0000000 --- a/apicaller/api_caller_auths.go +++ /dev/null @@ -1,216 +0,0 @@ -package apicaller - -import ( - "encoding/json" - "os" - "strings" - "sync" - "sync/atomic" - "unsafe" - - "repositories.action2quare.com/ayo/gocommon/flagx" -) - -type ApiCaller interface { - HasAuthority(authPath string) bool - GetMyAuthority() []string -} - -type ApiCallerAuths interface { - NewApiCaller(user string) ApiCaller - NewApiCallerByServer() ApiCaller - Update(newusers map[string]*map[string]bool) error - Serialize() []byte -} - -type apiCallerAuths struct { - sync.Mutex - serialized unsafe.Pointer // *[]byte - users map[string]*map[string]bool // email -> authoriries -} - -func (a *apiCallerAuths) Serialize() []byte { - btptr := atomic.LoadPointer(&a.serialized) - return *(*[]byte)(btptr) -} - -func (a *apiCallerAuths) getAuthority(email string) []string { - a.Lock() - defer a.Unlock() - - auths := a.users[email] - if auths == nil { - return nil - } - - var out []string - for k, v := range *auths { - if v { - out = append(out, k) - } - } - return out -} - -func (a *apiCallerAuths) Update(newAuths map[string]*map[string]bool) error { - src := map[string][]string{} - for user, auths := range newAuths { - for cat, has := range *auths { - if has { - arr := append(src[cat], user) - src[cat] = arr - } else if _, ok := src[cat]; !ok { - src[cat] = []string{} - } - } - } - - a.Lock() - defer a.Unlock() - - file, err := os.Create(*userAuthsFileName) - if err != nil { - return err - } - defer file.Close() - - enc := json.NewEncoder(file) - err = enc.Encode(src) - if err != nil { - return err - } - - a.users = newAuths - bt, _ := json.Marshal(newAuths) - atomic.StorePointer(&a.serialized, unsafe.Pointer(&bt)) - - return nil -} - -func (a *apiCallerAuths) hasAuthority(email string, authPath string) bool { - a.Lock() - defer a.Unlock() - - auths, ok := a.users[email] - if !ok { - return false - } - - if (*auths)[authPath] { - return true - } - - for k, v := range *auths { - if strings.HasPrefix(k, authPath+"/") { - return v - } - } - - return false -} - -var userAuthsFileName = flagx.String("userauth", "userauths.json", "-userauth=[json file path]") - -func NewApiCallerAuths() ApiCallerAuths { - var out apiCallerAuths - f, _ := os.Open(*userAuthsFileName) - if f == nil { - emptyAuths := map[string][]string{ - "/admins": {"enter_first_admin_email@action2quare.com"}, - } - newf, _ := os.Create(*userAuthsFileName) - if newf != nil { - enc := json.NewEncoder(newf) - enc.Encode(emptyAuths) - newf.Close() - - f, _ = os.Open(*userAuthsFileName) - } - } - - if f != nil { - defer f.Close() - - var src map[string][]string - dec := json.NewDecoder(f) - dec.Decode(&src) - - compiled := make(map[string]*map[string]bool) - - // 전체 유저 목록을 먼저 뽑고나서 - for _, users := range src { - for _, user := range users { - if _, ok := compiled[user]; !ok { - compiled[user] = &map[string]bool{} - } - } - } - // 전체 유저한테 모든 카테고리를 설정한다. - for _, auths := range compiled { - for cat := range src { - (*auths)[cat] = false - } - } - // 이제 유저별 권한을 설정 - for category, users := range src { - for _, user := range users { - (*compiled[user])[category] = true - } - } - - out = apiCallerAuths{ - users: compiled, - } - } else { - - out = apiCallerAuths{ - users: map[string]*map[string]bool{}, - } - } - - marshaled, _ := json.Marshal(out.users) - out.serialized = unsafe.Pointer(&marshaled) - - return &out -} - -type apiCaller struct { - userAuths *apiCallerAuths - caller string -} - -func (a *apiCallerAuths) NewApiCaller(user string) ApiCaller { - if len(user) == 0 { - return nil - } - return &apiCaller{ - userAuths: a, - caller: user, - } -} - -func (a *apiCallerAuths) NewApiCallerByServer() ApiCaller { - return &apiCaller{ - userAuths: a, - caller: "", - } -} - -func (ac apiCaller) callByServer() bool { - return len(ac.caller) == 0 -} - -func (ac apiCaller) HasAuthority(authPath string) bool { - if ac.callByServer() { - return true - } - - return ac.userAuths.hasAuthority(ac.caller, authPath) -} - -func (ac apiCaller) GetMyAuthority() []string { - if !ac.callByServer() { - return ac.userAuths.getAuthority(ac.caller) - } - return nil -} diff --git a/server.go b/server.go index 086aacf..675b7d8 100644 --- a/server.go +++ b/server.go @@ -826,6 +826,14 @@ func (hc *HttpApiBroker) AddHandler(receiver HttpApiHandler) { } } +func (hc *HttpApiBroker) AllMethods() (out []string) { + out = make([]string, 0, len(hc.methods)) + for name := range hc.methods { + out = append(out, name) + } + return +} + func (hc *HttpApiBroker) CallByHeader(w http.ResponseWriter, r *http.Request) { funcname := r.Header.Get("AS-X-CALL") if len(funcname) == 0 {