diff options
Diffstat (limited to 'src/oidc/main.go')
| -rw-r--r-- | src/oidc/main.go | 73 |
1 files changed, 57 insertions, 16 deletions
diff --git a/src/oidc/main.go b/src/oidc/main.go index fd80c0d..c996e6a 100644 --- a/src/oidc/main.go +++ b/src/oidc/main.go @@ -6,20 +6,6 @@ import ( "net/http" ) -func main() { - log.Println("Starting server, listening on port 8282.") - - server := &http.Server{ - Addr: ":8282", - Handler: http.HandlerFunc(handler), - ReadTimeout: 0, - WriteTimeout: 0, - IdleTimeout: 0, - } - - log.Fatal(server.ListenAndServe()) -} - type AuthorizationRequest struct { ResponseType string Scope string @@ -28,6 +14,20 @@ type AuthorizationRequest struct { RedirectUri string } +type TokenRequest struct { + GrantType string + Code string + RedirectUri string +} + +type TokenResponse struct { + AccessToken string + TokenType string + RefreshToken string + ExpiresIn int + IdToken string +} + func handler(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" && r.Method == "GET" { w.WriteHeader(http.StatusOK) @@ -40,9 +40,50 @@ func handler(w http.ResponseWriter, r *http.Request) { State: r.FormValue("state"), RedirectUri: r.FormValue("redirect_uri"), } - http.Redirect(w, r, fmt.Sprintf("%s?code=example&state=%s", ar.RedirectUri, ar.State), 302) + if ar.ResponseType == "code" { + url := fmt.Sprintf("%s?code=example&state=%s", ar.RedirectUri, ar.State) + http.Redirect(w, r, url, 302) + } else { + w.WriteHeader(http.StatusNotFound) + fmt.Fprintf(w, "Not Found\n") + } + } else if r.URL.Path == "/token" && r.Method == "POST" { + tr := &TokenRequest{ + GrantType: r.FormValue("grant_type"), + Code: r.FormValue("code"), + RedirectUri: r.FormValue("redirect_uri"), + } + r := &TokenResponse{ + AccessToken: "stateful_token", + TokenType: "Bearer", + RefreshToken: "another_stateful_token", + ExpiresIn: 3600, + IdToken: "JWT", + } + + if tr.GrantType == "authorization_code" { + w.Header().Set("Content-Type", "application/json") + fmt.Fprintf(w, `{"access_token": "%s","token_type": "%s","refresh_token": "%s","expires_in": %d,"id_token": "%s"}`, r.AccessToken, r.TokenType, r.RefreshToken, r.ExpiresIn, r.IdToken) + } else { + w.WriteHeader(http.StatusNotFound) + fmt.Fprintf(w, "Not Found\n") + } } else { - log.Printf("method: %s path: %s error: unsupported request\n", r.Method, r.URL.Path) w.WriteHeader(http.StatusNotFound) + fmt.Fprintf(w, "Not Found\n") + } +} + +func main() { + log.Println("Starting server, listening on port 8282.") + + server := &http.Server{ + Addr: ":8282", + Handler: http.HandlerFunc(handler), + ReadTimeout: 0, + WriteTimeout: 0, + IdleTimeout: 0, } + + log.Fatal(server.ListenAndServe()) } |
