-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding_test.go
More file actions
88 lines (70 loc) · 2.08 KB
/
Copy pathencoding_test.go
File metadata and controls
88 lines (70 loc) · 2.08 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
84
85
86
87
88
package cybersource
import (
"fmt"
"testing"
"github.com/golang-jwt/jwt/v5"
)
func TestDecodeJWE(t *testing.T) {
// This is a JWE token (5 parts) - the ENCRYPTED payload sent TO CyberSource
// NOT the capture context (which is a JWT with 3 parts)
jweToken := "JWE_TOKEN_HERE"
// Decode the JWE header to see the key ID used
header, err := DecodeJWEHeader(jweToken)
if err != nil {
t.Fatalf("Failed to decode JWE header: %v", err)
}
fmt.Printf("JWE Header:\n")
fmt.Printf(" Key ID (kid): %s\n", header.Kid)
fmt.Printf(" Algorithm: %s\n", header.Alg)
fmt.Printf(" Encryption: %s\n", header.Enc)
}
func TestParseCaptureContext(t *testing.T) {
// A proper capture context JWT has 3 parts (header.payload.signature)
// Example token:
captureContextJWT := `JWT_TOKEN_HERE`
ctx, err := ParseCaptureContext(captureContextJWT)
if err != nil {
t.Logf("Parse error: %v", err)
return
}
fmt.Printf("Parsed Capture Context:\n")
fmt.Printf(" Origin: %s\n", ctx.Payload.Flx.Origin)
fmt.Printf(" Path: %s\n", ctx.Payload.Flx.Path)
fmt.Printf(" Key ID: %s\n", ctx.Payload.Flx.JWK.Kid)
fmt.Printf(" Expired: %v\n", ctx.IsExpired())
fmt.Printf(" Token URL: %s\n", ctx.GetTokenURL())
}
func TestTokenizeCard(t *testing.T) {
client := NewFlexClient()
// You need a valid capture context from CyberSource
captureContextJWT := "JWT_TOKEN_HERE"
card := CardDetails{
Number: "4111111111111111",
SecurityCode: "123",
Type: string(CardTypeVisa),
ExpirationMonth: "12",
ExpirationYear: "2026",
}
token, err := client.TokenizeCard(captureContextJWT, card)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Token: %s\n", token)
}
func parsePaymentToken(encoded string) (string, error) {
token, _, err := jwt.NewParser().ParseUnverified(encoded, jwt.MapClaims{})
if err != nil {
return "", err
}
claims := token.Claims.(jwt.MapClaims)
jti, ok := claims["jti"]
if !ok {
return "", fmt.Errorf("token not found")
}
parsed, ok := jti.(string)
if !ok {
return "", fmt.Errorf("failed to parse token")
}
return parsed, nil
}