/
oauth2_prompt.go
63 lines (53 loc) · 1.84 KB
/
oauth2_prompt.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
package cmd
import "github.com/cloudentity/oauth2c/internal/oauth2"
func PromptForClientConfig(client oauth2.ClientConfig, server oauth2.ServerConfig) oauth2.ClientConfig {
// grant type
if client.GrantType == "" {
client.GrantType = PromptStringSlice("Grant type", server.SupportedGrantTypes)
}
// auth method
switch client.GrantType {
case oauth2.AuthorizationCodeGrantType, oauth2.ClientCredentialsGrantType, oauth2.RefreshTokenGrantType, oauth2.PasswordGrantType, oauth2.JWTBearerGrantType:
if client.AuthMethod == "" {
client.AuthMethod = PromptStringSlice("Token endpoint auth method", server.SupportedTokenEndpointAuthMethods)
}
}
// response types
switch client.GrantType {
case oauth2.AuthorizationCodeGrantType, oauth2.ImplicitGrantType:
if len(client.ResponseType) == 0 || client.ResponseType[0] == "" {
client.ResponseType = PromptMultiStringSlice("Response types", server.SupportedResponseTypes)
}
}
// response mode
switch client.GrantType {
case oauth2.AuthorizationCodeGrantType, oauth2.ImplicitGrantType:
if client.ResponseMode == "" {
client.ResponseMode = PromptStringSlice("Response mode", server.SupportedResponseModes)
}
}
if client.ClientID == "" {
client.ClientID = PromptString("Client ID")
}
// client secret
switch client.AuthMethod {
case oauth2.ClientSecretBasicAuthMethod, oauth2.ClientSecretPostAuthMethod, oauth2.ClientSecretJwtAuthMethod:
if client.ClientSecret == "" {
client.ClientSecret = PromptString("Client secret")
}
}
switch client.GrantType {
case oauth2.PasswordGrantType:
if client.Username == "" {
client.Username = PromptString("Username")
}
if client.Password == "" {
client.Password = PromptString("Password")
}
case oauth2.RefreshTokenGrantType:
if client.RefreshToken == "" {
client.RefreshToken = PromptString("Refresh token")
}
}
return client
}