bit of basic oauth code
This commit is contained in:
parent
e1b018c11e
commit
ac2600c21c
113
main.go
113
main.go
|
@ -5,6 +5,8 @@ import (
|
|||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
@ -16,22 +18,27 @@ type ServerConfig struct {
|
|||
Name string
|
||||
Homepage string
|
||||
}
|
||||
type AdminConfig struct {
|
||||
type PanelConfig struct {
|
||||
Host string
|
||||
WebRoot string
|
||||
Port int
|
||||
LogPath string
|
||||
}
|
||||
type AuthConfig struct {
|
||||
GiteaURL string
|
||||
ClientID string
|
||||
ClientSecret string
|
||||
AuthorizedUsers []string
|
||||
}
|
||||
type Config struct {
|
||||
Server ServerConfig
|
||||
Admin AdminConfig
|
||||
Panel PanelConfig
|
||||
Auth AuthConfig
|
||||
}
|
||||
|
||||
var config = new(Config)
|
||||
var redirecturi string
|
||||
var requesturl string
|
||||
|
||||
func loadConfig() (err error) {
|
||||
configfile, err := ioutil.ReadFile("config.yml")
|
||||
|
@ -40,38 +47,118 @@ func loadConfig() (err error) {
|
|||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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) {
|
||||
path := r.URL.Path
|
||||
var tmpl string
|
||||
if path == "/" {
|
||||
tmpl = "templates/home.html"
|
||||
} else {
|
||||
tmpl = fmt.Sprintf("templates/%s", path)
|
||||
loc := fmt.Sprintf("templates%s", path)
|
||||
|
||||
// var re = regexp.MustCompile(`\.(svg|jpg|jpeg|png|webp|ico|css|js)$`)
|
||||
|
||||
// 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() {
|
||||
err := loadConfig()
|
||||
fmt.Print(config)
|
||||
|
||||
if err != nil {
|
||||
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.StripPrefix("/resources/",
|
||||
http.FileServer(http.Dir("./resources"))))
|
||||
|
||||
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 {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,12 +4,12 @@
|
|||
|
||||
@layer base {
|
||||
.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 {
|
||||
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;
|
||||
}
|
||||
input {
|
||||
|
|
|
@ -12,18 +12,10 @@
|
|||
<body class="bg-slate-200 flex min-h-screen">
|
||||
<div class="login">
|
||||
<form>
|
||||
<h3 class="font-bold mb-3 text-lg">Log in to {{.Server.Name}}</h3>
|
||||
<div class="w-full my-2">
|
||||
<label for="username">Username</label>
|
||||
<input name="username" type="text">
|
||||
</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>
|
||||
<h3 class="font-bold mb-6 text-lg text-center">Log in to {{.Server.Name}} Admin</h3>
|
||||
<a href="/login" class="button my-10 px-16">
|
||||
Log In With Gitea
|
||||
</a>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
|
|
Reference in New Issue
Block a user