-
Notifications
You must be signed in to change notification settings - Fork 17
/
request.go
70 lines (55 loc) · 1.75 KB
/
request.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package clientCredsFlow
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/XeroAPI/xoauth/pkg/db"
"github.com/XeroAPI/xoauth/pkg/oidc"
"github.com/gookit/color"
)
type ClientCredsFlowInteractor struct {
wellKnownConfig oidc.WellKnownConfiguration
database *db.CredentialStore
operatingSystem string
}
func NewClientCredsFlow(wellKnownConfig oidc.WellKnownConfiguration, database *db.CredentialStore, operatingSystem string) ClientCredsFlowInteractor {
return ClientCredsFlowInteractor{
wellKnownConfig: wellKnownConfig,
database: database,
operatingSystem: operatingSystem,
}
}
func (interactor *ClientCredsFlowInteractor) Request(client db.OidcClient, dryRun bool) {
var scopes = strings.Join(client.Scopes, " ")
var tokenResult, tokenErr = oidc.RequestWithClientCredentials(interactor.wellKnownConfig.TokenEndpoint, client.ClientId, client.ClientSecret, scopes)
if tokenErr != nil {
log.Fatalln(tokenErr)
}
log.Println("Validating access token")
var _, validateErr = oidc.ValidateToken(tokenResult.AccessToken, interactor.wellKnownConfig, client.ClientId)
if validateErr != nil {
log.Fatalln(validateErr)
}
jsonData, jsonErr := json.MarshalIndent(tokenResult, "", " ")
log.Print("Storing tokens in local keychain")
_, tokenSaveErr := interactor.database.SaveTokens(client.Alias, oidc.TokenResultSet{
AccessToken: tokenResult.AccessToken,
ExpiresAt: tokenResult.ExpiresAt,
})
// Can fail with warning
if tokenSaveErr != nil {
log.Printf("%s: %v",
color.Yellow.Sprintf("failed to save tokens to keychain"),
tokenSaveErr,
)
}
if jsonErr != nil {
log.Fatalln(jsonErr)
}
_, finalWriteErr := fmt.Fprintln(os.Stdout, string(jsonData))
if finalWriteErr != nil {
log.Fatalln(finalWriteErr)
}
}