summaryrefslogtreecommitdiff
path: root/src/oidc/main.go
blob: 8df0d77d6502a4b4ef8bec37315303fa9603cbfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main

import (
	"fmt"
	"log"
	"net/http"
)

type AuthorizationRequest struct {
	ResponseType string
	Scope        string
	ClientId     string
	State        string
	RedirectUri  string
	Nonce        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)
		fmt.Fprintf(w, "Hello, world!\n")
	} else if r.URL.Path == "/authorize" && r.Method == "GET" {
		responseType := r.FormValue("response_type")
		if responseType == "code" {
			// Authorization Code Flow https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth
			ar := &AuthorizationRequest{
				ResponseType: r.FormValue("response_type"),
				Scope:        r.FormValue("scope"),
				ClientId:     r.FormValue("client_id"),
				State:        r.FormValue("state"),
				RedirectUri:  r.FormValue("redirect_uri"),
			}
			url := fmt.Sprintf("%s?code=example&state=%s", ar.RedirectUri, ar.State)
			http.Redirect(w, r, url, 302)
		} else if responseType == "id_token token" || responseType == "id_token" {
			// Implicit Flow https://openid.net/specs/openid-connect-core-1_0.html#ImplicitFlowAuth
			ar := &AuthorizationRequest{
				ResponseType: r.FormValue("response_type"),
				RedirectUri:  r.FormValue("redirect_uri"),
				Nonce:        r.FormValue("nonce"),
			}
			idToken := "jwt"
			url := fmt.Sprintf("%s?access_token=example&token_type=bearer&id_token=%s&expires_in=3600&state=%s", ar.RedirectUri, idToken, ar.State)
			http.Redirect(w, r, url, 302)
		} else if responseType == "code id_token" || responseType == "code token" || responseType == "code id_token token" {
			// Hybrid Flow https://openid.net/specs/openid-connect-core-1_0.html#HybridFlowAuth
			w.WriteHeader(http.StatusNotImplemented)
		} 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"),
		}
		if tr.GrantType == "authorization_code" {
			// Authorization Code Flow https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth
			r := &TokenResponse{
				AccessToken:  "stateful_token",
				TokenType:    "Bearer",
				RefreshToken: "another_stateful_token",
				ExpiresIn:    3600,
				IdToken:      "JWT",
			}

			w.Header().Set("Content-Type", "application/json")
			w.Header().Set("Cache-Control", "no-store")
			w.Header().Set("Pragma", "no-cache")
			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 {
		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())
}