-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
43 lines (34 loc) · 979 Bytes
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package strava
import (
"context"
"fmt"
"golang.org/x/oauth2"
)
func (c *Client) AuthRedirectURL() string {
return c.oacfg.RedirectURL
}
func (c *Client) AuthCodeURL() string {
return c.AuthCodeURLWithRedirectURL("")
}
func (c *Client) AuthCodeURLWithRedirectURL(redirectURL string) string {
oacfg := c.oacfg
if redirectURL != "" {
oacfg.RedirectURL = redirectURL
}
return oacfg.AuthCodeURL("state", oauth2.AccessTypeOffline)
}
func (c *Client) AuthExchange(ctx context.Context, code string) (uint, error) {
token, err := c.oacfg.Exchange(ctx, code)
if err != nil {
return 0, fmt.Errorf("could not exchange code for token: %w", err)
}
extra, ok := token.Extra("athlete").(map[string]any)
if !ok {
return 0, fmt.Errorf("could not get athlete data from token")
}
athleteID := uint(extra["id"].(float64))
if err := c.tstore.Save(ctx, athleteID, token); err != nil {
return 0, fmt.Errorf("could not save token: %w", err)
}
return athleteID, nil
}