-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnect.go
71 lines (60 loc) · 1.74 KB
/
connect.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
package auth_backend
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"golang.org/x/oauth2"
)
// Backend is the main part of auth_backend.
// It stores oauth2.Config, app and edx url configuration
type Backend struct {
conf *oauth2.Config
app_url string
edx_url string
}
// Init to initialize a new backend authentication
func Init(client_id, client_secret, app_url, edx_url string) *Backend {
return &Backend{
conf: &oauth2.Config{
ClientID: client_id,
ClientSecret: client_secret,
RedirectURL: app_url + URL_REDIRECT,
Scopes: Scopes(),
Endpoint: oauth2.Endpoint{
AuthURL: edx_url + URL_AUTHORIZATION,
TokenURL: edx_url + URL_ACCESS_TOKEN,
},
},
app_url: app_url,
edx_url: edx_url,
}
}
// GetAuthorizationURL return authentication url based on given state
func (backend *Backend) GetAuthorizationURL(state string) string {
return backend.conf.AuthCodeURL(state)
}
// Authenticate is callback handler when receiving redirection from edx site after successfully login
func (backend *Backend) Authenticate(receivedState string, values url.Values) (UserInfo, error) {
var user UserInfo
queryState := values.Get("state")
if receivedState != queryState {
return user, fmt.Errorf("Invalid session state: retrieved: %s; Param: %s", receivedState, queryState)
}
code := values.Get("code")
token, err := backend.conf.Exchange(oauth2.NoContext, code)
if err != nil {
return user, err
}
client := backend.conf.Client(oauth2.NoContext, token)
userinfo, err := client.Get(backend.edx_url + URL_USER_INFO)
if err != nil {
return user, err
}
defer userinfo.Body.Close()
data, _ := ioutil.ReadAll(userinfo.Body)
if err := json.Unmarshal(data, &user); err != nil {
return user, err
}
return user, nil
}