Skip to content

Commit 248f9c4

Browse files
committed
feat: added service account support in init
1 parent c89d7ba commit 248f9c4

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

phase/phase.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,44 @@ type DeleteSecretOptions struct {
6868

6969
// Init initializes a new instance of Phase with the provided service token, host, and debug flag.
7070
func Init(serviceToken, host string, debug bool) *Phase {
71-
// Validate the service token against the pattern.
72-
matches := misc.PssServicePattern.FindStringSubmatch(serviceToken)
73-
if matches == nil || len(matches) != 6 {
71+
72+
// Validate the service token against the pattern
73+
serviceMatches := misc.PssServicePattern.FindStringSubmatch(serviceToken)
74+
userMatches := misc.PssUserPattern.FindStringSubmatch(serviceToken)
75+
76+
// Check if it's a valid service token
77+
if serviceMatches == nil || len(serviceMatches) != 6 {
78+
if userMatches != nil {
79+
log.Fatalf("Error: User token provided. Expected service token.")
80+
}
7481
log.Fatalf("Error: Invalid Phase Service Token.")
7582
}
7683

77-
// Use default host if none is specified.
84+
// Use Phase Cloud if no host is provided
7885
if host == "" {
7986
host = misc.PhaseCloudAPIHost
8087
}
8188

82-
// Create a new Phase instance with parsed service token components and debug flag.
89+
// Get version from regex group 1 (the capture group for 'v(\d+)')
90+
version := serviceMatches[1] // This will be "2" for v2 tokens
91+
92+
// Determine token type based on version
93+
tokenType := "Service"
94+
if version == "2" {
95+
tokenType = "ServiceAccount"
96+
}
97+
98+
// Create a new Phase instance with parsed service token components and debug flag
8399
return &Phase{
84100
Prefix: "pss_service",
85-
PesVersion: matches[1],
86-
AppToken: matches[2],
87-
PssUserPublicKey: matches[3],
88-
Keyshare0: matches[4],
89-
Keyshare1UnwrapKey: matches[5],
101+
PesVersion: "v" + version,
102+
AppToken: serviceMatches[2],
103+
PssUserPublicKey: serviceMatches[3],
104+
Keyshare0: serviceMatches[4],
105+
Keyshare1UnwrapKey: serviceMatches[5],
90106
Host: host,
91107
Debug: debug,
108+
TokenType: tokenType,
92109
}
93110
}
94111

0 commit comments

Comments
 (0)