1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-01-10 04:17:59 +02:00
authboss/oauth2/providers.go

72 lines
1.7 KiB
Go
Raw Normal View History

package oauth2
import (
"encoding/json"
"net/http"
"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`
)
type googleMeResponse struct {
ID string `json:"id"`
Email string `json:"email"`
}
// testing
var clientGet = (*http.Client).Get
// 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)
resp, err := clientGet(client, googleInfoEndpoint)
if err != nil {
return nil, err
}
defer resp.Body.Close()
dec := json.NewDecoder(resp.Body)
var jsonResp googleMeResponse
if err = dec.Decode(&jsonResp); err != nil {
return nil, err
}
return authboss.Attributes{
authboss.StoreOAuth2UID: jsonResp.ID,
authboss.StoreEmail: jsonResp.Email,
}, nil
}
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
}