]> git.feebdaed.xyz Git - gomehub.git/commitdiff
admin id pw logic add
authorseantywork <seantywork@gmail.com>
Thu, 19 Dec 2024 09:53:29 +0000 (18:53 +0900)
committerseantywork <seantywork@gmail.com>
Thu, 19 Dec 2024 09:53:29 +0000 (18:53 +0900)
.gitmodules
Makefile
config.yaml.tmpl
ctl/base.go
ctl/config.go
ctl/ctl.go
pkg/auth/auth.go
pkg/auth/oauth2.go
public/js/index/signin.js
public/vendor/TuiCss [new submodule]
view/index/signinidiot.html [new file with mode: 0644]

index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0c295f598e25e1cb336703b7364c7c356e8d33dd 100644 (file)
@@ -0,0 +1,3 @@
+[submodule "public/vendor/TuiCss"]
+       path = public/vendor/TuiCss
+       url = https://github.com/vinibiavatti1/TuiCss.git
index e3d37f65565d12cee15aabc72b79fc1a14b7d723..0150adb08a542af7e7e9ee9f35396aeba1b027f6 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -11,6 +11,8 @@ build:
 
 vendor:
 
+       git submodule init
+
        cd public/vendor/TuiCss && git pull
 
 .PHONY: test
index 0a3626af9a46dddc1fdbe3d3dfbd93dd262b1293..5e74a2a5b8709a48c875478685debe3620d5951f 100644 (file)
@@ -22,6 +22,12 @@ maxFileSize: 1073741824
 # maybe too long?
 timeoutSec: 30
 
+auth: 
+  useOauth2: false
+  admins:
+    - id: hello
+      pw: example
+
 # this section is used to set
 # websocket port for generic communication with server
 # unrelated to WebRTC 
index 26adce50ca96baa86502f6ba0844ac28ad952041..254e535a2e7d93058fa5bfd9bfa606531c0de191 100644 (file)
@@ -48,6 +48,14 @@ func GetViewSignin(c *gin.Context) {
 
 }
 
+func GetViewSigninIdiot(c *gin.Context) {
+
+       pkgauth.WhoAmI(c)
+
+       c.HTML(200, "index/signinidiot.html", gin.H{})
+
+}
+
 func GetViewMypage(c *gin.Context) {
 
        _, my_type, _ := pkgauth.WhoAmI(c)
index ba90380acc8f32c419e272c8327479ce4786dacc..f97a062d702884c50cbbf2d94f4a16bce2e4ac07 100644 (file)
@@ -14,7 +14,14 @@ type SOLIAGAIN_CONFIG struct {
        ServePort   int    `yaml:"servePort"`
        MaxFileSize int64  `yaml:"maxFileSize"`
        TimeoutSec  int    `yaml:"timeoutSec"`
-       Com         struct {
+       Auth        struct {
+               UseOauth2 bool `yaml:"useOauth2"`
+               Admins    []struct {
+                       Id string `yaml:"id"`
+                       Pw string `yaml:"pw"`
+               } `yaml:"admins"`
+       } `yaml:"auth"`
+       Com struct {
                ChannelPort         int `yaml:"channelPort"`
                ChannelPortExternal int `yaml:"channelPortExternal"`
        } `yaml:"com"`
index de97239a556a84239b02bdbbd777752c7c5b0901..cf91ada516caefd7a67138702fbd71a9bafb8c72 100644 (file)
@@ -2,6 +2,7 @@ package controller
 
 import (
        "fmt"
+       "log"
        "time"
 
        "github.com/gin-gonic/contrib/sessions"
@@ -48,6 +49,26 @@ func ConfigureRuntime(e *gin.Engine) {
        //pkgsoli.INTERNAL_URL = CONF.InternalUrl
 
        pkgauth.DEBUG = CONF.Debug
+       pkgauth.USE_OAUTH2 = CONF.Auth.UseOauth2
+
+       adminslen := len(CONF.Auth.Admins)
+
+       admins := make(map[string]string)
+
+       for i := 0; i < adminslen; i++ {
+
+               admins[CONF.Auth.Admins[i].Id] = CONF.Auth.Admins[i].Pw
+
+       }
+
+       err := pkgauth.RegisterAdmins(admins)
+
+       if err != nil {
+
+               log.Fatalf("failed to register admins: %s", err.Error())
+
+               return
+       }
 
        pkgcom.CHANNEL_ADDR = CONF.ServeAddr
        pkgcom.CHANNEL_PORT = fmt.Sprintf("%d", CONF.Com.ChannelPort)
@@ -113,6 +134,8 @@ func RegisterRoutes(e *gin.Engine) {
 
        e.GET("/signin", GetViewSignin)
 
+       e.GET("/signin/idiot", GetViewSigninIdiot)
+
        e.GET("/mypage", GetViewMypage)
 
        e.GET("/mypage/article", GetViewMypageArticle)
@@ -135,6 +158,8 @@ func RegisterRoutes(e *gin.Engine) {
 
        e.GET("/oauth2/google/callback", pkgauth.OauthGoogleCallback)
 
+       e.POST("/api/auth/signin/idiot", pkgauth.LoginIdiot)
+
        e.GET("/api/auth/user/list", pkgauth.UserList)
 
        e.POST("/api/auth/user/add", pkgauth.UserAdd)
index c7f119c26a9294c35030de6361b5ca51db47c78a..8a0d7d91c68cf8e100c010e45c85d9a9678eaaf0 100644 (file)
@@ -4,6 +4,7 @@ import (
        "encoding/json"
        "fmt"
        "net/http"
+       "os"
 
        "github.com/gin-gonic/contrib/sessions"
        "github.com/gin-gonic/gin"
@@ -14,6 +15,10 @@ import (
 
 var DEBUG bool = false
 
+var USE_OAUTH2 bool = false
+
+var ADMINS = make(map[string]string)
+
 type UserCreate struct {
        Passphrase      string `json:"passphrase"`
        Id              string `json:"id"`
@@ -41,6 +46,40 @@ func GenerateStateAuthCookie(c *gin.Context) string {
        return state
 }
 
+func RegisterAdmins(admins map[string]string) error {
+
+       err := os.RemoveAll("./data/admin")
+
+       if err != nil {
+               return fmt.Errorf("failed to remove data/admin")
+       }
+
+       err = os.MkdirAll("./data/admin", 0755)
+
+       if err != nil {
+
+               return fmt.Errorf("failed to create data/admin")
+       }
+
+       for k, v := range admins {
+
+               ADMINS[k] = v
+
+               name := "./data/admin/" + k + ".json"
+
+               err := os.WriteFile(name, []byte("{}"), 0644)
+
+               if err != nil {
+
+                       return fmt.Errorf("failed to create data/admin: %s: %s", k, err.Error())
+               }
+
+       }
+
+       return nil
+
+}
+
 func OauthGoogleLogin(c *gin.Context) {
 
        my_key, my_type, _ := WhoAmI(c)
@@ -55,6 +94,13 @@ func OauthGoogleLogin(c *gin.Context) {
 
        }
 
+       if !USE_OAUTH2 {
+
+               c.Redirect(302, "/signin/idiot")
+
+               return
+       }
+
        oauth_state := GenerateStateAuthCookie(c)
 
        u := GoogleOauthConfig.AuthCodeURL(oauth_state)
@@ -454,3 +500,101 @@ func Logout(c *gin.Context) {
        c.JSON(http.StatusOK, com.SERVER_RE{Status: "success", Reply: "logged out"})
 
 }
+
+func LoginIdiot(c *gin.Context) {
+
+       my_key, my_type, my_id := WhoAmI(c)
+
+       if my_key != "" && my_type != "" {
+
+               dbquery.RemoveSessionKeyFromSession(my_key)
+
+               fmt.Printf("user login: removed existing id: %s\n", my_id)
+
+               return
+
+       }
+
+       var req com.CLIENT_REQ
+
+       var u_login UserLogin
+
+       if err := c.BindJSON(&req); err != nil {
+
+               fmt.Printf("user login: failed to bind: %s\n", err.Error())
+
+               c.JSON(http.StatusBadRequest, com.SERVER_RE{Status: "error", Reply: "invalid format"})
+
+               return
+       }
+
+       err := json.Unmarshal([]byte(req.Data), &u_login)
+
+       if err != nil {
+
+               fmt.Printf("user login: failed to unmarshal: %s\n", err.Error())
+
+               c.JSON(http.StatusBadRequest, com.SERVER_RE{Status: "error", Reply: "invalid format"})
+
+               return
+
+       }
+
+       if !VerifyDefaultValue(u_login.Id) {
+
+               fmt.Printf("user login: not valid id: %s\n", u_login.Id)
+
+               c.JSON(http.StatusBadRequest, com.SERVER_RE{Status: "error", Reply: "invalid format"})
+
+               return
+       }
+
+       as, err := dbquery.GetByIdFromAdmin(u_login.Id)
+
+       if as == nil {
+
+               fmt.Printf("user login: no such admin id: %s: %s\n", u_login.Id, err.Error())
+
+               c.JSON(http.StatusBadRequest, com.SERVER_RE{Status: "error", Reply: "invalid format"})
+
+               return
+       }
+
+       credPw, okay := ADMINS[u_login.Id]
+
+       if !okay {
+
+               fmt.Printf("user login: no such admin id in admins: %s\n", u_login.Id)
+
+               c.JSON(http.StatusBadRequest, com.SERVER_RE{Status: "error", Reply: "invalid format"})
+
+               return
+       }
+
+       if u_login.Passphrase != credPw {
+
+               fmt.Printf("user login: passphrase: %s", "not matching")
+
+               c.JSON(http.StatusForbidden, com.SERVER_RE{Status: "error", Reply: "passphrase not matching"})
+
+               return
+
+       }
+
+       session_key := GenerateStateAuthCookie(c)
+
+       err = dbquery.MakeSessionForAdmin(session_key, u_login.Id)
+
+       if err != nil {
+
+               fmt.Printf("user login: failed to get from user: %s", err.Error())
+
+               c.JSON(http.StatusInternalServerError, com.SERVER_RE{Status: "error", Reply: "failed to login"})
+
+               return
+
+       }
+
+       c.JSON(http.StatusOK, com.SERVER_RE{Status: "success", Reply: "logged in"})
+
+}
index c338efab3a100e442745f5beb72fe280de117e71..a97fc93f3e9eee1df9be002c8a10503832f0f64c 100644 (file)
@@ -39,6 +39,9 @@ var GoogleOauthConfig *oauth2.Config
 
 func InitAuth() {
 
+       if !USE_OAUTH2 {
+               return
+       }
        OAUTH_JSON = GetOAuthJSON()
 
        GoogleOauthConfig = GenerateGoogleOauthConfig()
index bafdc1c0dde4816d74edaa5c1943cc7016fac593..5bf203885441da5536123985514287b026674f73 100644 (file)
@@ -61,4 +61,65 @@ async function userSignin(){
     location.href = "/"
 
 
+}
+
+
+
+async function idiotSignin(){
+
+
+
+    let u_id = document.getElementById("user-id").value 
+
+    if(u_id == ""){
+  
+        alert("no user id")
+    
+        return
+    
+    }
+
+
+    let u_pw = document.getElementById("user-pw").value 
+
+
+    if(u_pw == ""){
+  
+        alert("no user pw")
+    
+        return
+    
+    }
+
+
+
+    let uc = JSON.parse(JSON.stringify(USER_LOGIN))
+
+    uc.id = u_id
+    uc.passphrase = u_pw
+
+    let req = {
+        data: JSON.stringify(uc)
+    }
+
+    let resp = await fetch(`/api/auth/signin/idiot`, {
+        body: JSON.stringify(req),
+        method: "POST"
+    })
+
+
+    let result = await resp.json()
+
+    if(result.status != "success"){
+
+        alert("failed to login")
+
+        return
+    }
+
+    alert("successfully logged in: " + result.reply)
+
+    location.href = "/"
+
+
 }
\ No newline at end of file
diff --git a/public/vendor/TuiCss b/public/vendor/TuiCss
new file mode 160000 (submodule)
index 0000000..6a021ec
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit 6a021ecc2abb1fbe6da62bd370d1f2a764da1195
diff --git a/view/index/signinidiot.html b/view/index/signinidiot.html
new file mode 100644 (file)
index 0000000..4e1b8d9
--- /dev/null
@@ -0,0 +1,70 @@
+{{ define "index/signinidiot.html" }}
+<!doctype html>
+<html class="tui-bg-blue-black">
+
+    <head>
+        <title> feebdaed.xyz </title>
+        <meta name="viewport" content="width=device-width, initial-scale=1.0">
+        <link rel="icon" type="image/x-icon" href="/public/image/favicon.ico">
+        <link rel="stylesheet" href="/public/vendor/TuiCss/dist/tuicss.min.css"/>
+        <script src="/public/vendor/TuiCss/dist/tuicss.min.js"></script>
+    </head>
+
+    <body>
+
+        <nav class="tui-nav">
+            <ul>
+                <li class="tui-dropdown">
+                    <span class="red-168-text">M</span>enu
+                    <div class="tui-dropdown-content">
+                          <ul>
+                            <li><a  href="/"> <span class="red-168-text">H</span>ome </a></li>
+                          </ul>
+                        </ul>
+                    </div>
+                </li>
+            </ul>
+        </nav>
+        <div class="container">
+            <div class="row">
+              <div class="col s12 m12 l12">idiot sign in</div>
+            </div>
+            <div class="row">
+                <div class="col s12 m12 l12">
+                    <div class="tui-window tui-scroll-white">
+                        <fieldset class="tui-fieldset tui-border-double">
+                            <legend>idiot sign in</legend>
+                            <fieldset class="tui-fieldset tui-border-dotted">
+                                <div>
+                                    <input class="tui-input purple-255" type='text' id='user-id'/>
+                                    <label for='user-id'>Enter Idiot ID</label>
+                                </div>
+                            </fieldset>
+                            <fieldset class="tui-fieldset tui-border-dotted">
+                                <div>
+                                    <input class="tui-input purple-255" type='password' id='user-pw'/>
+                                    <label for='user-pw'>Enter Idiot Password</label>
+                                </div>
+                            </fieldset>
+            
+                            <br/>
+                            <div>
+                                <input class="tui-button" type="button" onclick="idiotSignin()" value="user login">
+                            </div>
+            
+                        </fieldset>
+                    </div>
+                </div>
+            </div>
+        </div>
+        <div class="tui-statusbar">
+            <ul>
+                <li><a href="#"><span class="red-168-text">Click </span>Admin</a></li>
+            </ul>
+        </div>
+
+    <script src="/public/js/index/signin.js"></script>
+    </body>
+
+</html>
+{{ end }}
\ No newline at end of file