Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Dec 20, 2023
1 parent 8fa2c44 commit 1fc6b66
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 2 deletions.
17 changes: 17 additions & 0 deletions cmd/localdev/callback.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>OAuth local dev</title>
</head>
<body>
<h5>OAuth callback</h5>
<ul>
<li>Provider: {{.Provider}}</li>
<li>Name: {{.Name}}</li>
<li>DisplayName: {{.DisplayName}}</li>
<li>ExternalID: {{.ExternalID}}</li>
<li>AvatarURL: {{.AvatarURL}}</li>
</ul>
<a href="/">Start over</a>
</body>
</html>
16 changes: 16 additions & 0 deletions cmd/localdev/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<title>OAuth local dev</title>
</head>
<body>
<h5>OAuth login</h5>
<ul>
{{range $key, $value := .Logins}}
<li>
<a href="{{$value}}">{{$key}}</a>
</li>
{{end}}
</ul>
</body>
</html>
68 changes: 67 additions & 1 deletion cmd/localdev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,77 @@ package main

import (
"fmt"
"html/template"
"net/http"
"os"

"github.com/minetest-go/oauth"
)

const BaseURL = "http://localhost:8080"

type Model struct {
Logins map[oauth.ProviderType]string
}

var model = &Model{
Logins: make(map[oauth.ProviderType]string),
}

func index(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("cmd/localdev/index.html"))
tmpl.ExecuteTemplate(w, "index.html", model)
}

func callback(w http.ResponseWriter, r *http.Request, user *oauth.OauthUserInfo) error {
tmpl := template.Must(template.ParseFiles("cmd/localdev/callback.html"))
tmpl.ExecuteTemplate(w, "callback.html", user)
return nil
}

func setup_provider(path string, cfg *oauth.OAuthConfig) {
fmt.Printf("Registering oauth provider %s on path '%s'\n", cfg.Provider, path)
cfg.CallbackURL = fmt.Sprintf("%s%s", BaseURL, path)
handler := oauth.NewHandler(callback, cfg)
model.Logins[cfg.Provider] = handler.LoginURL()
http.Handle(path, handler)
}

func main() {
fmt.Println("Starting")
if os.Getenv("DISCORD_APP_ID") != "" {
setup_provider("/api/oauth_callback/discord", &oauth.OAuthConfig{
Provider: oauth.ProviderTypeDiscord,
ClientID: os.Getenv("DISCORD_APP_ID"),
Secret: os.Getenv("DISCORD_APP_SECRET"),
})
}

if os.Getenv("CDB_APP_ID") != "" {
setup_provider("/api/oauth_callback/cdb", &oauth.OAuthConfig{
Provider: oauth.ProviderTypeCDB,
ClientID: os.Getenv("CDB_APP_ID"),
Secret: os.Getenv("CDB_APP_SECRET"),
})
}

if os.Getenv("GITHUB_APP_ID") != "" {
setup_provider("/api/oauth_callback/github", &oauth.OAuthConfig{
Provider: oauth.ProviderTypeGithub,
ClientID: os.Getenv("GITHUB_APP_ID"),
Secret: os.Getenv("GITHUB_APP_SECRET"),
})
}

if os.Getenv("MESEHUB_APP_ID") != "" {
setup_provider("/api/oauth_callback/mesehub", &oauth.OAuthConfig{
Provider: oauth.ProviderTypeMesehub,
ClientID: os.Getenv("MESEHUB_APP_ID"),
Secret: os.Getenv("MESEHUB_APP_SECRET"),
})
}

http.HandleFunc("/", index)
fmt.Printf("Starting local oauth dev env on %s\n", BaseURL)

server := &http.Server{Addr: ":8080", Handler: nil}
err := server.ListenAndServe()
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ version: "3.6"

services:
oauth:
image: golang:1.21.4
image: golang:1.21.5
volumes:
- ".:/data"
- "go_dir:/go"
- "go_cache:/.cache"
Expand Down
34 changes: 34 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,40 @@ Supported providers:
* Github
* Discord

# Development

Create a "docker-compose.override.yml" with the following content:
```yml
version: "3.6"

services:
oauth:
environment:
- GITHUB_APP_ID=
- GITHUB_APP_SECRET=
- MESEHUB_APP_ID=
- MESEHUB_APP_SECRET=
- DISCORD_APP_ID=
- DISCORD_APP_SECRET=
- CDB_APP_ID=
- CDB_APP_SECRET=
```
Fill in the client-id's and secrets accordingly.
Callback-urls have to match the following list:
* ContentDB: `http://localhost:8080/api/oauth_callback/cdb`
* Mesehub: `http://localhost:8080/api/oauth_callback/mesehub`
* Github: `http://localhost:8080/api/oauth_callback/github`
* Discord: `http://localhost:8080/api/oauth_callback/discord`

Start the dev server:
```sh
docker-compose up
```

Navigate to http://localhost:8080 and try out the login links

# License

Code: `MIT`

0 comments on commit 1fc6b66

Please sign in to comment.