Commit 0815655
Changed files (4)
cmd
server
pkg
web
cmd/server/main.go
@@ -3,15 +3,20 @@ package main
import (
"log"
"net/http"
+ "os"
"mokhan.ca/xlgmokha/oauth/pkg/web"
)
func main() {
log.Println("Starting server, listening on port 8282.")
+ issuer, ok := os.LookupEnv("ISSUER")
+ if !ok {
+ issuer = "http://localhost:8282"
+ }
server := &http.Server{
Addr: ":8282",
- Handler: web.NewHandler(),
+ Handler: web.NewHandler(issuer),
ReadTimeout: 0,
WriteTimeout: 0,
IdleTimeout: 0,
public/openid-configuration.json → pkg/web/templates/openid-configuration.json
@@ -1,10 +1,10 @@
{
- "issuer": "{{.Host}}",
- "authorization_endpoint": "{{.Host}}/authorize",
- "token_endpoint": "{{.Host}}/token",
- "userinfo_endpoint": "{{.Host}}/userinfo",
- "jwks_uri": "{{.Host}}/.well-known/jwks.json",
- "revocation_endpoint": "{{.Host}}/revoke",
+ "issuer": "{{.Issuer}}",
+ "authorization_endpoint": "{{.Issuer}}/authorize",
+ "token_endpoint": "{{.Issuer}}/token",
+ "userinfo_endpoint": "{{.Issuer}}/userinfo",
+ "jwks_uri": "{{.Issuer}}/.well-known/jwks.json",
+ "revocation_endpoint": "{{.Issuer}}/revoke",
"scopes_supported": [
"openid"
],
pkg/web/http_mux.go
@@ -44,15 +44,17 @@ func (h *HttpContext) createIdToken(clientId string) string {
}
type HttpContext struct {
- log *log.Logger
+ issuer string
keyData []byte
+ log *log.Logger
}
-func NewHandler() http.Handler {
+func NewHandler(issuer string) http.Handler {
keyData, _ := ioutil.ReadFile("insecure.pem")
h := &HttpContext{
- log: log.Default(),
+ issuer: issuer,
keyData: keyData,
+ log: log.Default(),
}
mux := http.NewServeMux()
mux.Handle("/", http.HandlerFunc(h.Default))
pkg/web/well_known.go
@@ -2,28 +2,28 @@ package web
import (
"crypto/x509"
+ _ "embed"
"encoding/json"
"encoding/pem"
"fmt"
- "io/ioutil"
"net/http"
- "os"
"text/template"
"github.com/lestrrat-go/jwx/v2/jwk"
)
+//go:embed templates/openid-configuration.json
+var data string
+
+var (
+ tmpl = template.Must(template.New("").Parse(string(data)))
+)
+
func (h *HttpContext) WellKnown(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path)
if r.URL.Path == "/.well-known/openid-configuration" {
w.Header().Set("Content-Type", "application/json")
- data, _ := ioutil.ReadFile("./public/openid-configuration.json")
- tmpl, _ := template.New("test").Parse(string(data))
- host, ok := os.LookupEnv("HOST")
- if !ok {
- host = "http://localhost:8282"
- }
- tmpl.Execute(w, struct{ Host string }{Host: host})
+ tmpl.Execute(w, struct{ Issuer string }{Issuer: h.issuer})
} else if r.URL.Path == "/.well-known/jwks.json" {
w.Header().Set("Content-Type", "application/json")
privatePem, _ := pem.Decode(h.keyData)