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
|
package web
import (
"fmt"
"net/http"
"github.com/hashicorp/uuid"
"mokhan.ca/xlgmokha/oauth/pkg/dto"
)
func (h *HttpContext) Authorize(w http.ResponseWriter, r *http.Request) {
if 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 := &dto.AuthorizationRequest{
ResponseType: r.FormValue("response_type"),
Scope: r.FormValue("scope"),
ClientId: r.FormValue("client_id"),
State: r.FormValue("state"),
RedirectUri: r.FormValue("redirect_uri"),
}
code := uuid.GenerateUUID()
tokens[code] = uuid.GenerateUUID()
url := fmt.Sprintf("%s?code=%s&state=%s", ar.RedirectUri, code, 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 := &dto.AuthorizationRequest{
ResponseType: r.FormValue("response_type"),
RedirectUri: r.FormValue("redirect_uri"),
Nonce: r.FormValue("nonce"),
}
idToken := h.createIdToken(r.FormValue("client_id"))
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")
}
}
}
|