/
fake.go
115 lines (97 loc) · 2.69 KB
/
fake.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
package auth
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/markbates/goth"
"golang.org/x/oauth2"
)
// Mostly copied from github.com/markbates/goth/providers/faux.
// - Name is changed to fake.
// - ID is changed to 1.
// - Callback URL can be set when creating the provider.
// FakeProvider is used only for testing.
type FakeProvider struct {
HTTPClient *http.Client
Callback string
providerName string
}
// FakeSession is used only for testing.
type FakeSession struct {
ID string
Name string
Email string
AuthURL string
AccessToken string
}
// Name is used only for testing.
func (p *FakeProvider) Name() string {
return "fake"
}
// SetName is to update the name of the provider (needed in case of multiple providers of 1 type)
func (p *FakeProvider) SetName(name string) {
p.providerName = name
}
// BeginAuth is used only for testing.
func (p *FakeProvider) BeginAuth(state string) (goth.Session, error) {
c := &oauth2.Config{
Endpoint: oauth2.Endpoint{
AuthURL: p.Callback,
},
}
url := c.AuthCodeURL(state)
return &FakeSession{
ID: "1",
AuthURL: url,
}, nil
}
// FetchUser is used only for testing.
func (p *FakeProvider) FetchUser(session goth.Session) (goth.User, error) {
sess := session.(*FakeSession)
user := goth.User{
UserID: sess.ID,
Name: sess.Name,
Email: sess.Email,
Provider: p.Name(),
AccessToken: sess.AccessToken,
}
if user.AccessToken == "" {
return user, fmt.Errorf("%s cannot get user information without accessToken", p.providerName)
}
return user, nil
}
// UnmarshalSession is used only for testing.
func (p *FakeProvider) UnmarshalSession(data string) (goth.Session, error) {
sess := &FakeSession{}
err := json.NewDecoder(strings.NewReader(data)).Decode(sess)
return sess, err
}
// Client is used only for testing.
func (p *FakeProvider) Client() *http.Client {
return goth.HTTPClientWithFallBack(p.HTTPClient)
}
// Debug is used only for testing.
func (p *FakeProvider) Debug(debug bool) {}
// RefreshTokenAvailable is used only for testing
func (p *FakeProvider) RefreshTokenAvailable() bool {
return false
}
// RefreshToken is used only for testing
func (p *FakeProvider) RefreshToken(refreshToken string) (*oauth2.Token, error) {
return nil, nil
}
// Authorize is used only for testing.
func (s *FakeSession) Authorize(provider goth.Provider, params goth.Params) (string, error) {
s.AccessToken = "access"
return s.AccessToken, nil
}
// Marshal is used only for testing.
func (s *FakeSession) Marshal() string {
b, _ := json.Marshal(s)
return string(b)
}
// GetAuthURL is used only for testing.
func (s *FakeSession) GetAuthURL() (string, error) {
return s.AuthURL, nil
}