forked from oauth2-proxy/oauth2-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
76 lines (66 loc) · 1.78 KB
/
util.go
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
package providers
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"golang.org/x/oauth2"
)
const (
tokenTypeBearer = "Bearer"
tokenTypeToken = "token"
acceptHeader = "Accept"
acceptApplicationJSON = "application/json"
)
func makeAuthorizationHeader(prefix, token string, extraHeaders map[string]string) http.Header {
header := make(http.Header)
for key, value := range extraHeaders {
header.Add(key, value)
}
header.Set("Authorization", fmt.Sprintf("%s %s", prefix, token))
return header
}
func makeOIDCHeader(accessToken string) http.Header {
// extra headers required by the IDP when making authenticated requests
extraHeaders := map[string]string{
acceptHeader: acceptApplicationJSON,
}
return makeAuthorizationHeader(tokenTypeBearer, accessToken, extraHeaders)
}
func makeLoginURL(p *ProviderData, redirectURI, state string, extraParams url.Values) url.URL {
a := *p.LoginURL
params, _ := url.ParseQuery(a.RawQuery)
params.Set("redirect_uri", redirectURI)
params.Add("scope", p.Scope)
params.Set("client_id", p.ClientID)
params.Set("response_type", "code")
params.Add("state", state)
for n, p := range extraParams {
for _, v := range p {
params.Add(n, v)
}
}
a.RawQuery = params.Encode()
return a
}
// getIDToken extracts an IDToken stored in the `Extra` fields of an
// oauth2.Token
func getIDToken(token *oauth2.Token) string {
idToken, ok := token.Extra("id_token").(string)
if !ok {
return ""
}
return idToken
}
// formatGroup coerces an OIDC groups claim into a string
// If it is non-string, marshal it into JSON.
func formatGroup(rawGroup interface{}) (string, error) {
if group, ok := rawGroup.(string); ok {
return group, nil
}
jsonGroup, err := json.Marshal(rawGroup)
if err != nil {
return "", err
}
return string(jsonGroup), nil
}