bit of basic oauth code

This commit is contained in:
leah 2022-01-27 23:16:13 +00:00
parent e1b018c11e
commit ac2600c21c
3 changed files with 106 additions and 27 deletions

113
main.go
View File

@ -5,6 +5,8 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"os"
"strings"
"text/template" "text/template"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@ -16,22 +18,27 @@ type ServerConfig struct {
Name string Name string
Homepage string Homepage string
} }
type AdminConfig struct { type PanelConfig struct {
Host string
WebRoot string WebRoot string
Port int Port int
LogPath string LogPath string
} }
type AuthConfig struct { type AuthConfig struct {
GiteaURL string GiteaURL string
ClientID string
ClientSecret string
AuthorizedUsers []string AuthorizedUsers []string
} }
type Config struct { type Config struct {
Server ServerConfig Server ServerConfig
Admin AdminConfig Panel PanelConfig
Auth AuthConfig Auth AuthConfig
} }
var config = new(Config) var config = new(Config)
var redirecturi string
var requesturl string
func loadConfig() (err error) { func loadConfig() (err error) {
configfile, err := ioutil.ReadFile("config.yml") configfile, err := ioutil.ReadFile("config.yml")
@ -40,38 +47,118 @@ func loadConfig() (err error) {
} }
err = yaml.Unmarshal(configfile, &config) err = yaml.Unmarshal(configfile, &config)
// init oauth bits
redirecturi = fmt.Sprintf("%s/login/endpoint", config.Panel.Host)
requesturl = fmt.Sprintf("%s/login/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code&state=STATE",
strings.Trim(config.Auth.GiteaURL, " "),
strings.Trim(config.Auth.ClientID, " "),
strings.Trim(redirecturi, " "))
return err return err
} }
func openLogFile(logfile string) {
if logfile != "" {
lf, err := os.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0640)
if err != nil {
log.Fatal("OpenLogfile: os.OpenFile:", err)
}
log.SetOutput(lf)
}
}
func logRequest(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path path := r.URL.Path
var tmpl string loc := fmt.Sprintf("templates%s", path)
if path == "/" {
tmpl = "templates/home.html" // var re = regexp.MustCompile(`\.(svg|jpg|jpeg|png|webp|ico|css|js)$`)
} else {
tmpl = fmt.Sprintf("templates/%s", path) // if re.MatchString(path) {
// http.StripPrefix("/resources/",
// http.FileServer(http.Dir("./resources")))
// return
// }
exists, err := exists(loc)
if err != nil {
log.Fatal(err)
} }
t, _ := template.ParseFiles(tmpl)
t.Execute(w, config) if !exists {
http.NotFound(w, r)
return
}
var tmpl string
switch path {
case "/":
tmpl = "templates/home.html"
t, _ := template.ParseFiles(tmpl)
t.Execute(w, config)
default:
tmpl = fmt.Sprintf("templates%s", path)
t, _ := template.ParseFiles(tmpl)
t.Execute(w, config)
}
}
func loginHandler(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/login", "/login/":
http.Redirect(w, r, requesturl, http.StatusFound)
case "/login/endpoint", "/login/endpoint/":
loginEndpoint(w, r)
}
}
func loginEndpoint(w http.ResponseWriter, r *http.Request) {
token := r.FormValue("code")
fmt.Print(token)
} }
func main() { func main() {
err := loadConfig() err := loadConfig()
fmt.Print(config)
if err != nil { if err != nil {
log.Fatalf("couldn't load config: %s", err) log.Fatalf("couldn't load config: %s", err)
} }
openLogFile(config.Panel.LogPath)
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
http.HandleFunc("/login/", loginHandler)
http.Handle("/resources/", http.Handle("/resources/",
http.StripPrefix("/resources/", http.StripPrefix("/resources/",
http.FileServer(http.Dir("./resources")))) http.FileServer(http.Dir("./resources"))))
http.HandleFunc("/", handler) http.HandleFunc("/", handler)
err = http.ListenAndServe(":8080", nil) fmt.Printf("listening on %v\n", config.Panel.Port)
fmt.Printf("Logging to %v\n", config.Panel.LogPath)
err = http.ListenAndServe(fmt.Sprintf(":%d", config.Panel.Port), logRequest(http.DefaultServeMux))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -4,12 +4,12 @@
@layer base { @layer base {
.login { .login {
@apply flex flex-row justify-center items-center m-auto shadow-md bg-slate-50 p-6 rounded px-10 border-t-4 border-t-emerald-700 @apply flex flex-row justify-center items-center m-auto shadow-md bg-slate-50 p-6 pb-8 rounded px-10 border-t-4 border-t-emerald-700
} }
} }
@layer components { @layer components {
button { .button {
@apply w-full bg-gradient-to-t from-emerald-800 to-emerald-700 text-slate-100 rounded py-2 shadow-sm shadow-emerald-800 hover:opacity-95 font-bold; @apply w-full bg-gradient-to-t from-emerald-800 to-emerald-700 text-slate-100 rounded py-2 shadow-sm shadow-emerald-800 hover:opacity-95 font-bold;
} }
input { input {

View File

@ -12,18 +12,10 @@
<body class="bg-slate-200 flex min-h-screen"> <body class="bg-slate-200 flex min-h-screen">
<div class="login"> <div class="login">
<form> <form>
<h3 class="font-bold mb-3 text-lg">Log in to {{.Server.Name}}</h3> <h3 class="font-bold mb-6 text-lg text-center">Log in to {{.Server.Name}} Admin</h3>
<div class="w-full my-2"> <a href="/login" class="button my-10 px-16">
<label for="username">Username</label> Log In With Gitea
<input name="username" type="text"> </a>
</div>
<div class="w-full my-2">
<label for="password">Password</label>
<input name="password" type="password">
</div>
<button class="my-2">
Log In
</button>
</form> </form>
</div> </div>
</body> </body>