-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathauth.go
133 lines (111 loc) · 3.03 KB
/
auth.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package steam_go
import (
"errors"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
)
var (
steam_login = "https://steamcommunity.com/openid/login"
openId_mode = "checkid_setup"
openId_ns = "http://specs.openid.net/auth/2.0"
openId_identifier = "http://specs.openid.net/auth/2.0/identifier_select"
validation_regexp = regexp.MustCompile("^(http|https)://steamcommunity.com/openid/id/[0-9]{15,25}$")
digits_extraction_regexp = regexp.MustCompile("\\D+")
)
type OpenId struct {
root string
returnUrl string
data url.Values
}
func NewOpenId(r *http.Request) *OpenId {
id := new(OpenId)
proto := "http://"
if r.TLS != nil {
proto = "https://"
}
id.root = proto + r.Host
uri := r.RequestURI
if i := strings.Index(uri, "openid"); i != -1 {
uri = uri[0 : i-1]
}
id.returnUrl = id.root + uri
switch r.Method {
case "POST":
id.data = r.Form
case "GET":
id.data = r.URL.Query()
}
return id
}
func (id OpenId) AuthUrl() string {
data := map[string]string{
"openid.claimed_id": openId_identifier,
"openid.identity": openId_identifier,
"openid.mode": openId_mode,
"openid.ns": openId_ns,
"openid.realm": id.root,
"openid.return_to": id.returnUrl,
}
i := 0
url := steam_login + "?"
for key, value := range data {
url += key + "=" + value
if i != len(data)-1 {
url += "&"
}
i++
}
return url
}
func (id *OpenId) ValidateAndGetId() (string, error) {
if id.Mode() != "id_res" {
return "", errors.New("Mode must equal to \"id_res\".")
}
if id.data.Get("openid.return_to") != id.returnUrl {
return "", errors.New("The \"return_to url\" must match the url of current request.")
}
params := make(url.Values)
params.Set("openid.assoc_handle", id.data.Get("openid.assoc_handle"))
params.Set("openid.signed", id.data.Get("openid.signed"))
params.Set("openid.sig", id.data.Get("openid.sig"))
params.Set("openid.ns", id.data.Get("openid.ns"))
split := strings.Split(id.data.Get("openid.signed"), ",")
for _, item := range split {
params.Set("openid."+item, id.data.Get("openid."+item))
}
params.Set("openid.mode", "check_authentication")
resp, err := http.PostForm(steam_login, params)
if err != nil {
return "", err
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
response := strings.Split(string(content), "\n")
if response[0] != "ns:"+openId_ns {
return "", errors.New("Wrong ns in the response.")
}
if strings.HasSuffix(response[1], "false") {
return "", errors.New("Unable validate openId.")
}
openIdUrl := id.data.Get("openid.claimed_id")
if !validation_regexp.MatchString(openIdUrl) {
return "", errors.New("Invalid steam id pattern.")
}
return digits_extraction_regexp.ReplaceAllString(openIdUrl, ""), nil
}
func (id OpenId) ValidateAndGetUser(apiKey string) (*PlayerSummaries, error) {
steamId, err := id.ValidateAndGetId()
if err != nil {
return nil, err
}
return GetPlayerSummaries(steamId, apiKey)
}
func (id OpenId) Mode() string {
return id.data.Get("openid.mode")
}