-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
27 lines (23 loc) · 861 Bytes
/
client.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
package oauth2dev
import (
"context"
"fmt"
"golang.org/x/oauth2"
)
// HandleAuthorizationResponseFunc is a function to handle an authorization response.
// It should display the user code and verification URL to the user.
// See https://www.rfc-editor.org/rfc/rfc8628#section-3.3
type HandleAuthorizationResponseFunc func(response AuthorizationResponse)
// GetToken sends an authorization request and then polls the token response.
func GetToken(ctx context.Context, cfg oauth2.Config, h HandleAuthorizationResponseFunc) (*oauth2.Token, error) {
authorizationResponse, err := RetrieveCode(ctx, cfg)
if err != nil {
return nil, fmt.Errorf("authorization request: %w", err)
}
h(*authorizationResponse)
token, err := PollToken(ctx, cfg, *authorizationResponse)
if err != nil {
return nil, fmt.Errorf("poll token: %w", err)
}
return token, nil
}