2015-03-30 19:30:27 +00:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
2015-06-23 17:23:47 +00:00
|
|
|
"bytes"
|
2015-03-30 19:30:27 +00:00
|
|
|
"encoding/base64"
|
2015-05-21 03:23:48 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2015-06-23 17:23:47 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2015-06-23 11:23:39 +00:00
|
|
|
"log"
|
2015-06-23 17:23:47 +00:00
|
|
|
"net/http"
|
2015-03-30 19:30:27 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2015-06-23 11:23:39 +00:00
|
|
|
"time"
|
2015-03-30 19:30:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type GoogleProvider struct {
|
|
|
|
*ProviderData
|
2015-06-23 17:23:47 +00:00
|
|
|
RedeemRefreshUrl *url.URL
|
2015-03-30 19:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGoogleProvider(p *ProviderData) *GoogleProvider {
|
2015-03-31 16:59:07 +00:00
|
|
|
p.ProviderName = "Google"
|
2015-03-30 19:30:27 +00:00
|
|
|
if p.LoginUrl.String() == "" {
|
|
|
|
p.LoginUrl = &url.URL{Scheme: "https",
|
|
|
|
Host: "accounts.google.com",
|
2015-06-23 17:23:47 +00:00
|
|
|
Path: "/o/oauth2/auth",
|
|
|
|
// to get a refresh token. see https://developers.google.com/identity/protocols/OAuth2WebServer#offline
|
|
|
|
RawQuery: "access_type=offline",
|
|
|
|
}
|
2015-03-30 19:30:27 +00:00
|
|
|
}
|
|
|
|
if p.RedeemUrl.String() == "" {
|
|
|
|
p.RedeemUrl = &url.URL{Scheme: "https",
|
2015-06-23 17:23:47 +00:00
|
|
|
Host: "www.googleapis.com",
|
|
|
|
Path: "/oauth2/v3/token"}
|
2015-03-30 19:30:27 +00:00
|
|
|
}
|
2015-05-08 21:13:35 +00:00
|
|
|
if p.ValidateUrl.String() == "" {
|
|
|
|
p.ValidateUrl = &url.URL{Scheme: "https",
|
|
|
|
Host: "www.googleapis.com",
|
|
|
|
Path: "/oauth2/v1/tokeninfo"}
|
|
|
|
}
|
2015-03-30 19:30:27 +00:00
|
|
|
if p.Scope == "" {
|
|
|
|
p.Scope = "profile email"
|
|
|
|
}
|
|
|
|
return &GoogleProvider{ProviderData: p}
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
func emailFromIdToken(idToken string) (string, error) {
|
2015-05-21 03:23:48 +00:00
|
|
|
|
2015-03-30 19:30:27 +00:00
|
|
|
// id_token is a base64 encode ID token payload
|
|
|
|
// https://developers.google.com/accounts/docs/OAuth2Login#obtainuserinfo
|
2015-06-23 11:23:39 +00:00
|
|
|
jwt := strings.Split(idToken, ".")
|
2015-03-30 19:30:27 +00:00
|
|
|
b, err := jwtDecodeSegment(jwt[1])
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-05-21 03:23:48 +00:00
|
|
|
|
|
|
|
var email struct {
|
2015-06-23 11:23:39 +00:00
|
|
|
Email string `json:"email"`
|
|
|
|
EmailVerified bool `json:"email_verified"`
|
2015-03-30 19:30:27 +00:00
|
|
|
}
|
2015-05-21 03:23:48 +00:00
|
|
|
err = json.Unmarshal(b, &email)
|
2015-03-30 19:30:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-05-21 03:23:48 +00:00
|
|
|
if email.Email == "" {
|
|
|
|
return "", errors.New("missing email")
|
|
|
|
}
|
2015-06-23 11:23:39 +00:00
|
|
|
if !email.EmailVerified {
|
|
|
|
return "", fmt.Errorf("email %s not listed as verified", email.Email)
|
|
|
|
}
|
2015-05-21 03:23:48 +00:00
|
|
|
return email.Email, nil
|
2015-03-30 19:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func jwtDecodeSegment(seg string) ([]byte, error) {
|
|
|
|
if l := len(seg) % 4; l > 0 {
|
|
|
|
seg += strings.Repeat("=", 4-l)
|
|
|
|
}
|
|
|
|
|
|
|
|
return base64.URLEncoding.DecodeString(seg)
|
|
|
|
}
|
2015-05-13 01:48:13 +00:00
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
func (p *GoogleProvider) Redeem(redirectUrl, code string) (s *SessionState, err error) {
|
2015-06-23 17:23:47 +00:00
|
|
|
if code == "" {
|
|
|
|
err = errors.New("missing code")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
params := url.Values{}
|
|
|
|
params.Add("redirect_uri", redirectUrl)
|
|
|
|
params.Add("client_id", p.ClientID)
|
|
|
|
params.Add("client_secret", p.ClientSecret)
|
|
|
|
params.Add("code", code)
|
|
|
|
params.Add("grant_type", "authorization_code")
|
|
|
|
var req *http.Request
|
|
|
|
req, err = http.NewRequest("POST", p.RedeemUrl.String(), bytes.NewBufferString(params.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-06-23 11:23:39 +00:00
|
|
|
var body []byte
|
2015-06-23 17:23:47 +00:00
|
|
|
body, err = ioutil.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
err = fmt.Errorf("got %d from %q %s", resp.StatusCode, p.RedeemUrl.String(), body)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonResponse struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
2015-06-23 11:23:39 +00:00
|
|
|
ExpiresIn int64 `json:"expires_in"`
|
|
|
|
IdToken string `json:"id_token"`
|
2015-06-23 17:23:47 +00:00
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &jsonResponse)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-06-23 11:23:39 +00:00
|
|
|
var email string
|
|
|
|
email, err = emailFromIdToken(jsonResponse.IdToken)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s = &SessionState{
|
|
|
|
AccessToken: jsonResponse.AccessToken,
|
|
|
|
ExpiresOn: time.Now().Add(time.Duration(jsonResponse.ExpiresIn) * time.Second).Truncate(time.Second),
|
|
|
|
RefreshToken: jsonResponse.RefreshToken,
|
|
|
|
Email: email,
|
|
|
|
}
|
2015-06-23 17:23:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
func (p *GoogleProvider) RefreshSessionIfNeeded(s *SessionState) (bool, error) {
|
|
|
|
if s == nil || s.ExpiresOn.After(time.Now()) || s.RefreshToken == "" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newToken, duration, err := p.redeemRefreshToken(s.RefreshToken)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
origExpiration := s.ExpiresOn
|
|
|
|
s.AccessToken = newToken
|
|
|
|
s.ExpiresOn = time.Now().Add(duration).Truncate(time.Second)
|
|
|
|
log.Printf("refreshed access token %s (expired on %s)", s, origExpiration)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GoogleProvider) redeemRefreshToken(refreshToken string) (token string, expires time.Duration, err error) {
|
2015-06-23 17:23:47 +00:00
|
|
|
// https://developers.google.com/identity/protocols/OAuth2WebServer#refresh
|
|
|
|
params := url.Values{}
|
|
|
|
params.Add("client_id", p.ClientID)
|
|
|
|
params.Add("client_secret", p.ClientSecret)
|
|
|
|
params.Add("refresh_token", refreshToken)
|
|
|
|
params.Add("grant_type", "refresh_token")
|
|
|
|
var req *http.Request
|
|
|
|
req, err = http.NewRequest("POST", p.RedeemUrl.String(), bytes.NewBufferString(params.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var body []byte
|
|
|
|
body, err = ioutil.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
err = fmt.Errorf("got %d from %q %s", resp.StatusCode, p.RedeemUrl.String(), body)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-23 11:23:39 +00:00
|
|
|
var data struct {
|
2015-06-23 17:23:47 +00:00
|
|
|
AccessToken string `json:"access_token"`
|
2015-06-23 11:23:39 +00:00
|
|
|
ExpiresIn int64 `json:"expires_in"`
|
2015-06-23 17:23:47 +00:00
|
|
|
}
|
2015-06-23 11:23:39 +00:00
|
|
|
err = json.Unmarshal(body, &data)
|
2015-06-23 17:23:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-06-23 11:23:39 +00:00
|
|
|
token = data.AccessToken
|
|
|
|
expires = time.Duration(data.ExpiresIn) * time.Second
|
|
|
|
return
|
2015-06-23 17:23:47 +00:00
|
|
|
}
|