-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (74 loc) · 2.33 KB
/
main.go
File metadata and controls
83 lines (74 loc) · 2.33 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
// CLI example with auto-detection of browser availability.
//
// If a browser is available (local machine), it uses Authorization Code + PKCE.
// If not (SSH session), it falls back to Device Code flow.
// Tokens are persisted to OS keyring (with file fallback) for reuse.
//
// Usage:
//
// export AUTHGATE_URL=https://auth.example.com
// export CLIENT_ID=your-client-id
// go run main.go
package main
import (
"context"
"fmt"
"log"
"os"
authgate "github.com/go-authgate/sdk-go"
"github.com/go-authgate/sdk-go/oauth"
"github.com/joho/godotenv"
)
func main() {
_ = godotenv.Load()
authgateURL := os.Getenv("AUTHGATE_URL")
clientID := os.Getenv("CLIENT_ID")
if authgateURL == "" || clientID == "" {
log.Fatal("Set AUTHGATE_URL and CLIENT_ID")
}
ctx := context.Background()
client, token, err := authgate.New(ctx,
authgateURL,
clientID,
authgate.WithScopes("profile", "email"),
)
if err != nil {
log.Fatal(err)
}
printTokenInfo(ctx, client, token)
}
func maskToken(s string) string {
if len(s) <= 8 {
return "****"
}
return s[:8] + "..."
}
func printTokenInfo(ctx context.Context, client *oauth.Client, token *oauth.Token) {
info, err := client.UserInfo(ctx, token.AccessToken)
if err != nil {
fmt.Printf("Token: %s (UserInfo error: %v)\n", maskToken(token.AccessToken), err)
return
}
fmt.Printf("User: %s (%s)\n", info.Name, info.Email)
fmt.Printf("Subject: %s\n", info.Sub)
fmt.Printf("Access Token: %s\n", maskToken(token.AccessToken))
fmt.Printf("Refresh Token: %s\n", maskToken(token.RefreshToken))
fmt.Printf("Token Type: %s\n", token.TokenType)
fmt.Printf("Expires In: %d\n", token.ExpiresIn)
fmt.Printf("Expires At: %s\n", token.ExpiresAt)
fmt.Printf("Scope: %s\n", token.Scope)
fmt.Printf("ID Token: %s\n", maskToken(token.IDToken))
// Fetch token info for detailed scope and metadata
tokenInfo, err := client.TokenInfoRequest(ctx, token.AccessToken)
if err != nil {
fmt.Printf("TokenInfo error: %v\n", err)
return
}
fmt.Printf("TokenInfo Active: %v\n", tokenInfo.Active)
fmt.Printf("TokenInfo UserID: %s\n", tokenInfo.UserID)
fmt.Printf("TokenInfo ClientID: %s\n", tokenInfo.ClientID)
fmt.Printf("TokenInfo Scope: %s\n", tokenInfo.Scope)
fmt.Printf("TokenInfo SubjectType: %s\n", tokenInfo.SubjectType)
fmt.Printf("TokenInfo Issuer: %s\n", tokenInfo.Iss)
fmt.Printf("TokenInfo Exp: %d\n", tokenInfo.Exp)
}