2019-12-10 14:15:27 +00:00
|
|
|
package google
|
|
|
|
|
2019-12-28 14:38:38 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"go-cal/pkg/config"
|
|
|
|
"go-cal/pkg/types"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
2019-12-10 14:15:27 +00:00
|
|
|
|
|
|
|
var baseURL = "https://www.googleapis.com/"
|
|
|
|
|
|
|
|
func getURL(p string) (string, error) {
|
|
|
|
u, err := url.Parse(baseURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
u, err = u.Parse(p)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return u.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHeaders() map[string]string {
|
|
|
|
obj := make(map[string]string)
|
|
|
|
// conf := config.Get()
|
|
|
|
|
|
|
|
return obj
|
|
|
|
}
|
2019-12-28 14:38:38 +00:00
|
|
|
|
|
|
|
// OAuthExchangeCodeToToken -
|
|
|
|
func OAuthExchangeCodeToToken(code string) (token types.GoogleAuthToken, err error) {
|
|
|
|
conf := config.Get()
|
|
|
|
if conf == nil {
|
|
|
|
return token, errors.New("config not init")
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(conf.Google.TokenURL)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
qs := url.Values{}
|
|
|
|
qs.Set("code", code)
|
|
|
|
qs.Set("client_id", conf.Google.ClientID)
|
|
|
|
qs.Set("client_secret", conf.Google.ClientSecret)
|
|
|
|
qs.Set("redirect_uri", conf.Google.RedirectURL)
|
|
|
|
qs.Set("grant_type", "authorization_code")
|
|
|
|
|
|
|
|
reader := bytes.NewReader([]byte(qs.Encode()))
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", fmt.Sprintf("%s", u.String()), reader)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(b, &token)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// OAuthRefreshToken -
|
|
|
|
func OAuthRefreshToken(refreshToken string) (token string, expiresIn int64, err error) {
|
|
|
|
if len(refreshToken) == 0 {
|
|
|
|
return "", 0, errors.New("refresh token is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := config.Get()
|
|
|
|
if conf == nil {
|
|
|
|
return "", 0, errors.New("config not init")
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(conf.Google.TokenURL)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
qs := url.Values{}
|
|
|
|
qs.Set("client_id", conf.Google.ClientID)
|
|
|
|
qs.Set("client_secret", conf.Google.ClientSecret)
|
|
|
|
qs.Set("grant_type", "refresh_token")
|
|
|
|
|
|
|
|
reader := bytes.NewReader([]byte(qs.Encode()))
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", u.String(), reader)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var tokenData types.GoogleAuthToken
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&tokenData)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tokenData.ExpiresIn += time.Now().Unix()
|
|
|
|
|
|
|
|
return tokenData.AccessToken, tokenData.ExpiresIn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTokenInfo -
|
|
|
|
func GetTokenInfo(token string) (expire int64, err error) {
|
|
|
|
u, err := url.Parse("https://oauth2.googleapis.com/tokeninfo")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body := struct {
|
|
|
|
Azp string `json:"azp"`
|
|
|
|
Aud string `json:"aud"`
|
|
|
|
Scope string `json:"scope"`
|
|
|
|
Exp json.Number `json:"exp"`
|
|
|
|
ExpiresIn json.Number `json:"expires_in"`
|
|
|
|
AccessType string `json:"access_type"`
|
|
|
|
}{}
|
|
|
|
|
|
|
|
qs := url.Values{}
|
|
|
|
qs.Set("access_token", token)
|
|
|
|
|
|
|
|
resp, err := http.Get(fmt.Sprintf("%s?%s", u.String(), qs.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
expTime, err := body.Exp.Int64()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return expTime, nil
|
|
|
|
}
|