Newer
Older
// Package mocks provide mocks for development purposes (debug mode)
package mocks
import (
"fmt"
"net/http"
"os"
"strconv"
"forge.grandlyon.com/gestion-des-assemblees/elections/pkg/middlewares"
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
)
const literralContentType = "Content-Type"
const literralApplicationJson = "application/json"
var (
hostname = os.Getenv("HOSTNAME")
port string
)
// Init initialize the configuration
func Init(portFromMain int) {
port = strconv.Itoa(portFromMain)
}
// CreateMockOAuth2 creates a mock OAuth2 serve mux for development purposes
func CreateMockOAuth2() *http.ServeMux {
mux := http.NewServeMux()
// Returns authorization code back to the user
mux.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
redir := query.Get("redirect_uri") + "?state=" + query.Get("state") + "&code=mock_code"
http.Redirect(w, r, redir, 302)
})
// Returns authorization code back to the user, but without the provided state
mux.HandleFunc("/auth-wrong-state", func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
redir := query.Get("redirect_uri") + "?state=" + "a-random-state" + "&code=mock_code"
http.Redirect(w, r, redir, 302)
})
// Returns access token back to the user
mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(literralContentType, "application/x-www-form-urlencoded")
w.Write([]byte("access_token=mocktoken&scope=user&token_type=bearer"))
})
// Returns userinfo back to the user
mux.HandleFunc("/userinfo", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(literralContentType, literralApplicationJson)
w.Write([]byte(`{
"displayName": "Us ER",
"memberOf": [
"CN=USERS",
"CN=OTHER_GROUP"
],
"id": "1000",
"login": "USER"
}`))
})
// Returns userinfo back to the user (with an admin user)
mux.HandleFunc("/admininfo", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(literralContentType, literralApplicationJson)
w.Write([]byte(`{
"displayName": "Ad MIN",
"memberOf": [
"CN=ADMINS",
"CN=OTHER_GROUP"
],
"id": "1",
"login": "ADMIN"
}`))
})
// Logout
mux.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Logout OK")
})
return mux
}
// CreateMockAPI creates a mock OAuth2 serve mux for development purposes
func CreateMockAPI() *http.ServeMux {
mux := http.NewServeMux()
// Returns authorization code back to the user
frameSource := "https://static." + hostname + ":" + port
mux.Handle("/", middlewares.Cors(func() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(literralContentType, literralApplicationJson)
w.Write([]byte(`{
"foo": "bar",
"bar": "foo"
}`))
})
}(), frameSource))
return mux
}