/
facebook.go
117 lines (97 loc) · 4.23 KB
/
facebook.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
package facebook
import (
"github.com/stretchr/gomniauth"
"github.com/stretchr/gomniauth/common"
"github.com/stretchr/gomniauth/oauth2"
"github.com/stretchr/objx"
"net/http"
)
const (
facebookDefaultScope string = "email"
facebookName string = "facebook"
facebookDisplayName string = "Facebook"
facebookAuthURL string = "https://www.facebook.com/dialog/oauth"
facebookTokenURL string = "https://graph.facebook.com/oauth/access_token"
facebookEndpointProfile string = "https://graph.facebook.com/me?fields=email,first_name,last_name,link,about,id,name,picture,location"
)
// FacebookProvider implements the Provider interface and provides Facebook
// OAuth2 communication capabilities.
type FacebookProvider struct {
config *common.Config
tripperFactory common.TripperFactory
}
func New(clientId, clientSecret, redirectUrl string) *FacebookProvider {
p := new(FacebookProvider)
p.config = &common.Config{Map: objx.MSI(
oauth2.OAuth2KeyAuthURL, facebookAuthURL,
oauth2.OAuth2KeyTokenURL, facebookTokenURL,
oauth2.OAuth2KeyClientID, clientId,
oauth2.OAuth2KeySecret, clientSecret,
oauth2.OAuth2KeyRedirectUrl, redirectUrl,
oauth2.OAuth2KeyScope, facebookDefaultScope,
oauth2.OAuth2KeyAccessType, oauth2.OAuth2AccessTypeOnline,
oauth2.OAuth2KeyApprovalPrompt, oauth2.OAuth2ApprovalPromptAuto,
oauth2.OAuth2KeyResponseType, oauth2.OAuth2KeyCode)}
return p
}
// TripperFactory gets an OAuth2TripperFactory
func (provider *FacebookProvider) TripperFactory() common.TripperFactory {
if provider.tripperFactory == nil {
provider.tripperFactory = new(oauth2.OAuth2TripperFactory)
}
return provider.tripperFactory
}
// PublicData gets a public readable view of this provider.
func (provider *FacebookProvider) PublicData(options map[string]interface{}) (interface{}, error) {
return gomniauth.ProviderPublicData(provider, options)
}
// Name is the unique name for this provider.
func (provider *FacebookProvider) Name() string {
return facebookName
}
// DisplayName is the human readable name for this provider.
func (provider *FacebookProvider) DisplayName() string {
return facebookDisplayName
}
// GetBeginAuthURL gets the URL that the client must visit in order
// to begin the authentication process.
//
// The state argument contains anything you wish to have sent back to your
// callback endpoint.
// The options argument takes any options used to configure the auth request
// sent to the provider. In the case of OAuth2, the options map can contain:
// 1. A "scope" key providing the desired scope(s). It will be merged with the default scope.
func (provider *FacebookProvider) GetBeginAuthURL(state *common.State, options objx.Map) (string, error) {
if options != nil {
scope := oauth2.MergeScopes(options.Get(oauth2.OAuth2KeyScope).Str(), facebookDefaultScope)
provider.config.Set(oauth2.OAuth2KeyScope, scope)
}
return oauth2.GetBeginAuthURLWithBase(provider.config.Get(oauth2.OAuth2KeyAuthURL).Str(), state, provider.config)
}
// Get makes an authenticated request and returns the data in the
// response as a data map.
func (provider *FacebookProvider) Get(creds *common.Credentials, endpoint string) (objx.Map, error) {
return oauth2.Get(provider, creds, endpoint)
}
// GetUser uses the specified common.Credentials to access the users profile
// from the remote provider, and builds the appropriate User object.
func (provider *FacebookProvider) GetUser(creds *common.Credentials) (common.User, error) {
profileData, err := provider.Get(creds, facebookEndpointProfile)
if err != nil {
return nil, err
}
// build user
user := NewUser(profileData, creds, provider)
return user, nil
}
// CompleteAuth takes a map of arguments that are used to
// complete the authorisation process, completes it, and returns
// the appropriate Credentials.
func (provider *FacebookProvider) CompleteAuth(data objx.Map) (*common.Credentials, error) {
return oauth2.CompleteAuth(provider.TripperFactory(), data, provider.config, provider)
}
// GetClient returns an authenticated http.Client that can be used to make requests to
// protected facebook resources
func (provider *FacebookProvider) GetClient(creds *common.Credentials) (*http.Client, error) {
return oauth2.GetClient(provider.TripperFactory(), creds, provider)
}