diff options
| author | Max Resnick <max@ofmax.li> | 2020-11-08 11:45:16 -0800 |
|---|---|---|
| committer | Max Resnick <max@ofmax.li> | 2021-01-01 10:50:14 -0800 |
| commit | a397341ad471cc761f7fb930d77e53cf7eb40a2a (patch) | |
| tree | 76fb8318269569687fdd30467dc61ecba3499d09 /internal/goog | |
| parent | 689a57ec4a444f8233fe2e5ec7ceb0903218218d (diff) | |
| download | iserv-a397341ad471cc761f7fb930d77e53cf7eb40a2a.tar.gz | |
adds casbin and accounts
Diffstat (limited to 'internal/goog')
| -rw-r--r-- | internal/goog/goog.go | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/internal/goog/goog.go b/internal/goog/goog.go new file mode 100644 index 0000000..5102a95 --- /dev/null +++ b/internal/goog/goog.go @@ -0,0 +1,89 @@ +package goog + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + + "golang.org/x/oauth2" +) + +var ( + ProfileURL = "https://www.googleapis.com/oauth2/v3/userinfo" + ProfileKeyFmt = "goog:%s" +) + +// GoogleProfile auth'd user profile +// ProfileId, Email, Name, PictureURL +type GoogleProfile struct { + ProfileID string `json:"sub"` + Email string `json:"email"` + Name string `json:"name"` + PictureURL string `json:"picture"` +} + +// ProfileKeys conform to ProfileKeys +func (gp *GoogleProfile) ProfileKeyValues() (string, []string) { + return fmt.Sprintf(ProfileKeyFmt, gp.Email), []string{ + "profile_id", gp.ProfileID, + "email", gp.Email, + "name", gp.Name, + "picture_url", gp.PictureURL, + } +} + +type Servicer interface { + Config() *oauth2.Config + Profile(client *http.Client) (*GoogleProfile, *oauth2.Token, error) + UserClient(ctx context.Context, token *oauth2.Token) *http.Client +} + +// NewService container for interacting with Google +func NewService(o *oauth2.Config) Servicer { + return &Goog{ + o, + } + +} + +type Goog struct { + oauthConfig *oauth2.Config +} + +func (g *Goog) Config() *oauth2.Config { + return g.oauthConfig +} + +// UserCient just calls oauth2.Client +func (g *Goog) UserClient(ctx context.Context, token *oauth2.Token) *http.Client { + return g.oauthConfig.Client(ctx, token) +} + +// Profile get profile from google +func (g *Goog) Profile(client *http.Client) (*GoogleProfile, *oauth2.Token, error) { + client.Get(ProfileURL) + resp, err := client.Get(ProfileURL) + if err != nil { + return &GoogleProfile{}, &oauth2.Token{}, err + } + defer resp.Body.Close() + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return &GoogleProfile{}, &oauth2.Token{}, err + } + // Google Profile + gp := &GoogleProfile{} + err = json.Unmarshal(data, gp) + if err != nil { + return &GoogleProfile{}, &oauth2.Token{}, err + } + // Token + token := &oauth2.Token{} + err = json.Unmarshal(data, token) + if err != nil { + return &GoogleProfile{}, &oauth2.Token{}, err + } + return gp, token, nil +} |