2015-03-13 04:20:36 +02:00
|
|
|
package oauth2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-03-13 11:15:58 +02:00
|
|
|
"net/http"
|
2015-03-13 04:20:36 +02:00
|
|
|
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"gopkg.in/authboss.v0"
|
|
|
|
)
|
|
|
|
|
2015-08-04 00:25:06 +02:00
|
|
|
const (
|
|
|
|
googleInfoEndpoint = `https://www.googleapis.com/userinfo/v2/me`
|
|
|
|
facebookInfoEndpoint = `https://graph.facebook.com/me?fields=name,email`
|
2015-03-13 04:20:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type googleMeResponse struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
}
|
|
|
|
|
2015-03-13 11:15:58 +02:00
|
|
|
// testing
|
|
|
|
var clientGet = (*http.Client).Get
|
|
|
|
|
2015-03-13 04:20:36 +02:00
|
|
|
// Google is a callback appropriate for use with Google's OAuth2 configuration.
|
2015-04-01 00:27:47 +02:00
|
|
|
func Google(cfg oauth2.Config, token *oauth2.Token) (authboss.Attributes, error) {
|
|
|
|
client := cfg.Client(oauth2.NoContext, token)
|
2015-03-13 11:15:58 +02:00
|
|
|
resp, err := clientGet(client, googleInfoEndpoint)
|
2015-03-13 04:20:36 +02:00
|
|
|
if err != nil {
|
2015-03-14 01:23:43 +02:00
|
|
|
return nil, err
|
2015-03-13 04:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
var jsonResp googleMeResponse
|
|
|
|
if err = dec.Decode(&jsonResp); err != nil {
|
2015-03-14 01:23:43 +02:00
|
|
|
return nil, err
|
2015-03-13 04:20:36 +02:00
|
|
|
}
|
|
|
|
|
2015-03-14 01:23:43 +02:00
|
|
|
return authboss.Attributes{
|
|
|
|
authboss.StoreOAuth2UID: jsonResp.ID,
|
|
|
|
authboss.StoreEmail: jsonResp.Email,
|
|
|
|
}, nil
|
2015-03-13 04:20:36 +02:00
|
|
|
}
|
2015-08-03 22:35:43 +02:00
|
|
|
|
|
|
|
type facebookMeResponse struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
2015-08-03 22:35:43 +02:00
|
|
|
// Google is a callback appropriate for use with Google's OAuth2 configuration.
|
2015-08-03 22:35:43 +02:00
|
|
|
func Facebook(cfg oauth2.Config, token *oauth2.Token) (authboss.Attributes, error) {
|
|
|
|
client := cfg.Client(oauth2.NoContext, token)
|
|
|
|
resp, err := clientGet(client, facebookInfoEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
dec := json.NewDecoder(resp.Body)
|
|
|
|
var jsonResp facebookMeResponse
|
|
|
|
if err = dec.Decode(&jsonResp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return authboss.Attributes{
|
|
|
|
"name": jsonResp.Name,
|
|
|
|
authboss.StoreOAuth2UID: jsonResp.ID,
|
|
|
|
authboss.StoreEmail: jsonResp.Email,
|
|
|
|
}, nil
|
|
|
|
}
|