+[submodule "public/vendor/TuiCss"]
+ path = public/vendor/TuiCss
+ url = https://github.com/vinibiavatti1/TuiCss.git
vendor:
+ git submodule init
+
cd public/vendor/TuiCss && git pull
.PHONY: test
# 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
}
+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)
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"`
import (
"fmt"
+ "log"
"time"
"github.com/gin-gonic/contrib/sessions"
//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)
e.GET("/signin", GetViewSignin)
+ e.GET("/signin/idiot", GetViewSigninIdiot)
+
e.GET("/mypage", GetViewMypage)
e.GET("/mypage/article", GetViewMypageArticle)
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)
"encoding/json"
"fmt"
"net/http"
+ "os"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
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"`
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)
}
+ if !USE_OAUTH2 {
+
+ c.Redirect(302, "/signin/idiot")
+
+ return
+ }
+
oauth_state := GenerateStateAuthCookie(c)
u := GoogleOauthConfig.AuthCodeURL(oauth_state)
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"})
+
+}
func InitAuth() {
+ if !USE_OAUTH2 {
+ return
+ }
OAUTH_JSON = GetOAuthJSON()
GoogleOauthConfig = GenerateGoogleOauthConfig()
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
--- /dev/null
+Subproject commit 6a021ecc2abb1fbe6da62bd370d1f2a764da1195
--- /dev/null
+{{ 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