@@ -107,16 +107,7 @@ func TokenViaListener(baseUrl string, admin bool, cmd string, port int, timeout
107
107
108
108
// If we are done, send the result to the channel.
109
109
if completed {
110
- rp := TokenResult {}
111
- if len (tokenBytes ) == 0 {
112
- rp .err = errors .New ("canceled" )
113
- } else {
114
- err = json .Unmarshal (tokenBytes , & rp )
115
- if err != nil {
116
- message := fmt .Sprintf ("%s: cannot unmarshal response from JSON: %s" , "/v2/callbackWithOtp" , err .Error ())
117
- rp .err = errors .New (message )
118
- }
119
- }
110
+ rp := getTokenResult (tokenBytes )
120
111
done <- rp
121
112
}
122
113
})
@@ -125,11 +116,7 @@ func TokenViaListener(baseUrl string, admin bool, cmd string, port int, timeout
125
116
}()
126
117
127
118
// Open the browser.
128
- adminFlag := ""
129
- if admin {
130
- adminFlag = "&isAdmin=true"
131
- }
132
- url := fmt .Sprintf ("%s/app/user/verify-token?localAppName=%s&localPort=%d%s&redirect=true" , baseUrl , cmd , localPort , adminFlag )
119
+ url := getInteractiveUrl (admin , baseUrl , cmd , localPort )
133
120
err = open .Run (url )
134
121
DieIf (err , "failed to open interactive browser session" )
135
122
@@ -142,6 +129,29 @@ func TokenViaListener(baseUrl string, admin bool, cmd string, port int, timeout
142
129
}
143
130
}
144
131
132
+ func getTokenResult (tokenBytes []byte ) TokenResult {
133
+ tokenResult := TokenResult {}
134
+ if len (tokenBytes ) == 0 {
135
+ tokenResult .err = errors .New ("canceled" )
136
+ } else {
137
+ err := json .Unmarshal (tokenBytes , & tokenResult )
138
+ if err != nil {
139
+ message := fmt .Sprintf ("%s: cannot unmarshal response from JSON: %s" , "/v2/callbackWithOtp" , err .Error ())
140
+ tokenResult .err = errors .New (message )
141
+ }
142
+ }
143
+ return tokenResult
144
+ }
145
+
146
+ func getInteractiveUrl (admin bool , baseUrl string , cmd string , localPort int ) string {
147
+ adminFlag := ""
148
+ if admin {
149
+ adminFlag = "&isAdmin=true"
150
+ }
151
+ url := fmt .Sprintf ("%s/app/user/verify-token?localAppName=%s&localPort=%d%s&redirect=true" , baseUrl , cmd , localPort , adminFlag )
152
+ return url
153
+ }
154
+
145
155
func MustTokenInteractive (host string , admin bool , cmd string , port int ) (tokenResult TokenResult ) {
146
156
tokenResult = TokenViaListener (host , admin , cmd , port , 180 * time .Second )
147
157
DieIf (tokenResult .err , "failed to get token from interactive browser session" )
0 commit comments