2015-03-13 04:20:36 +02:00
|
|
|
package oauth2
|
|
|
|
|
|
|
|
import (
|
2017-02-21 01:56:26 +02:00
|
|
|
"context"
|
2015-03-13 04:20:36 +02:00
|
|
|
"encoding/json"
|
2018-03-09 04:39:51 +02:00
|
|
|
"io/ioutil"
|
2015-03-13 11:15:58 +02:00
|
|
|
"net/http"
|
2015-03-13 04:20:36 +02:00
|
|
|
|
2018-03-09 04:39:51 +02:00
|
|
|
"github.com/pkg/errors"
|
2017-02-21 01:56:26 +02:00
|
|
|
"golang.org/x/oauth2"
|
2015-03-13 04:20:36 +02:00
|
|
|
)
|
|
|
|
|
2018-03-09 04:39:51 +02:00
|
|
|
// Constants for returning in the FindUserDetails call
|
|
|
|
const (
|
|
|
|
OAuth2UID = "uid"
|
|
|
|
OAuth2Email = "email"
|
|
|
|
OAuth2Name = "name"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
2018-03-09 04:39:51 +02:00
|
|
|
// GoogleUserDetails can be used as a FindUserDetails function for an authboss.OAuth2Provider
|
|
|
|
func GoogleUserDetails(ctx context.Context, cfg oauth2.Config, token *oauth2.Token) (map[string]string, error) {
|
2015-09-11 16:03:05 +02:00
|
|
|
client := cfg.Client(ctx, 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()
|
2018-03-09 04:39:51 +02:00
|
|
|
byt, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to read body from google oauth2 endpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
var response googleMeResponse
|
|
|
|
if err = json.Unmarshal(byt, &response); err != nil {
|
2015-03-14 01:23:43 +02:00
|
|
|
return nil, err
|
2015-03-13 04:20:36 +02:00
|
|
|
}
|
|
|
|
|
2017-02-21 01:56:26 +02:00
|
|
|
return map[string]string{
|
2018-03-09 04:39:51 +02:00
|
|
|
OAuth2UID: response.ID,
|
|
|
|
OAuth2Email: response.Email,
|
2015-03-14 01:23:43 +02:00
|
|
|
}, 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"`
|
|
|
|
}
|
|
|
|
|
2018-03-09 04:39:51 +02:00
|
|
|
// FacebookUserDetails can be used as a FindUserDetails function for an authboss.OAuth2Provider
|
|
|
|
func FacebookUserDetails(ctx context.Context, cfg oauth2.Config, token *oauth2.Token) (map[string]string, error) {
|
2015-09-11 16:03:05 +02:00
|
|
|
client := cfg.Client(ctx, token)
|
2015-08-03 22:35:43 +02:00
|
|
|
resp, err := clientGet(client, facebookInfoEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
2018-03-09 04:39:51 +02:00
|
|
|
byt, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to read body from facebook oauth2 endpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
var response facebookMeResponse
|
|
|
|
if err = json.Unmarshal(byt, &response); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to parse json from facebook oauth2 endpoint")
|
2015-08-03 22:35:43 +02:00
|
|
|
}
|
|
|
|
|
2017-02-21 01:56:26 +02:00
|
|
|
return map[string]string{
|
2018-03-09 04:39:51 +02:00
|
|
|
OAuth2UID: response.ID,
|
|
|
|
OAuth2Email: response.Email,
|
|
|
|
OAuth2Name: response.Name,
|
2015-08-03 22:35:43 +02:00
|
|
|
}, nil
|
|
|
|
}
|