forked from yohcop/openid-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredirect.go
59 lines (50 loc) · 1.77 KB
/
redirect.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
package openid
import (
"net/url"
"strings"
)
func RedirectURL(id, callbackURL, realm string) (string, error) {
return redirectURL(id, callbackURL, realm, urlGetter, nil)
}
func RedirectURLWithExtraValues(id, callbackURL, realm string, extraValues *url.Values) (string, error) {
return redirectURL(id, callbackURL, realm, urlGetter, extraValues)
}
func redirectURL(id, callbackURL, realm string, getter httpGetter, extraValues *url.Values) (string, error) {
opEndpoint, opLocalID, claimedID, err := discover(id, getter)
if err != nil {
return "", err
}
return buildRedirectURL(opEndpoint, opLocalID, claimedID, callbackURL, realm, extraValues)
}
func buildRedirectURL(opEndpoint, opLocalID, claimedID, returnTo, realm string, extraValues *url.Values) (string, error) {
values := make(url.Values)
values.Add("openid.ns", "http://specs.openid.net/auth/2.0")
values.Add("openid.mode", "checkid_setup")
values.Add("openid.return_to", returnTo)
if len(claimedID) > 0 {
values.Add("openid.claimed_id", claimedID)
if len(opLocalID) > 0 {
values.Add("openid.identity", opLocalID)
} else {
values.Add("openid.identity",
"http://specs.openid.net/auth/2.0/identifier_select")
}
} else {
values.Add("openid.identity",
"http://specs.openid.net/auth/2.0/identifier_select")
}
if len(realm) > 0 {
values.Add("openid.realm", realm)
}
// Add in any extra values that might have been provided by the
// original caller. These are likely part of an extension asking
// for additional data
encodedExtraValues := ""
if extraValues != nil {
encodedExtraValues = "&" + extraValues.Encode()
}
if strings.Contains(opEndpoint, "?") {
return opEndpoint + "&" + values.Encode() + encodedExtraValues, nil
}
return opEndpoint + "?" + values.Encode() + encodedExtraValues, nil
}