Skip to content
Permalink
Browse files
cleared authorization param manage
  • Loading branch information
jcabrerizo committed May 31, 2019
1 parent d3d4f0e commit fe8d285ce690ec53cc0794e08fd57540b0dbb49d
Showing 4 changed files with 29 additions and 70 deletions.
@@ -66,12 +66,11 @@ func (cmd *Login) Metadata() command_metadata.CommandMetadata {
Flags: []cli.Flag{
cli.BoolFlag{Name: skipSslChecksParam, Usage: "Skip SSL Checks"},
cli.StringFlag{Name: authorizationParam+", A", Usage: "Type of authentication header. Format: 'authorization=Basic'" +
" or 'authorization=Bearer:xxxxx.yyyyyy.zzzzz'"},
" or 'authorization=Bearer:<JWT-token>'"},
},
}
}


func (cmd *Login) promptAndReadUsername() (username string) {
for username == "" {
reader := bufio.NewReader(os.Stdin)
@@ -95,13 +94,7 @@ func (cmd *Login) promptAndReadPassword() (password string) {
return string(bytePassword)
}

func (cmd *Login) getCredentialsFromCommandLineIfNeeded() {
if !cmd.isBasicAuth() {
return
}
if cmd.network.Credentials != ""{
return
}
func (cmd *Login) getCredentialsFromCommandLine() {

// Prompt for username if not supplied
if cmd.brooklynUser == "" {
@@ -116,36 +109,6 @@ func (cmd *Login) getCredentialsFromCommandLineIfNeeded() {
cmd.network.Credentials = base64.StdEncoding.EncodeToString([]byte(cmd.brooklynUser + ":" + cmd.brooklynPass))
}

func (cmd *Login) getCredentialsFromAuthParamIfNeeded(authParam string) {
if cmd.isBasicAuth() {
return
}
if cmd.network.Credentials=="" {
credentials := cmd.parseAuthorizationParam(authParam)
if len(credentials) != 2 {
error_handler.ErrorExit("Use authorization=type:value")
} else {
cmd.network.Credentials = credentials[1]
}
}
}

func (cmd *Login) getAuthorizationType(authorizationParamInput string) (authorizationType string) {
if authorizationParamInput !=""{
if strings.EqualFold(BASIC_AUTH,authorizationParamInput){
authorizationType = BASIC_AUTH
}else {
authorizationType = cmd.parseAuthorizationParam(authorizationParamInput)[0]
if strings.EqualFold(authorizationType,BEARER_AUTH) {
authorizationType=BEARER_AUTH
}
}
}else {
// default authentication
authorizationType = BASIC_AUTH
}
return authorizationType
}

func (cmd *Login) Run(scope scope.Scope, c *cli.Context) {
if !c.Args().Present() {
@@ -157,11 +120,23 @@ func (cmd *Login) Run(scope scope.Scope, c *cli.Context) {
cmd.brooklynUser = c.Args().Get(1)
cmd.brooklynPass = c.Args().Get(2)
cmd.network.SkipSslChecks = c.Bool("skipSslChecks")
cmd.network.AuthorizationType = cmd.getAuthorizationType(c.String(authorizationParam))

//clear Credentials credentials
//clear credentials
cmd.network.Credentials=""

authParamValue := c.String(authorizationParam)
if authParamValue != "" {
parts := strings.SplitN(authParamValue, ":", 2)
if len(parts) == 2 && strings.EqualFold(parts[0], BEARER_AUTH) {
cmd.network.AuthorizationType = BEARER_AUTH
cmd.network.Credentials = parts[1]
} else {
cmd.network.AuthorizationType = BASIC_AUTH
}
} else {
cmd.network.AuthorizationType = BASIC_AUTH
}

config := io.GetConfig()

if err := net.VerifyLoginURL(cmd.network); err != nil {
@@ -182,14 +157,14 @@ func (cmd *Login) Run(scope scope.Scope, c *cli.Context) {
cmd.network.Credentials = credentials
}
if authorizationType, err := config.GetAuthType(cmd.network.BrooklynUrl); err == nil{
// TODO remove
//fmt.Printf("authorizationType from file %s\n",authorizationType)
cmd.network.AuthorizationType = authorizationType
}
}

cmd.getCredentialsFromCommandLineIfNeeded()
cmd.getCredentialsFromAuthParamIfNeeded(c.String(authorizationParam))
fmt.Printf("AuthorizationType: %s, Credentials: %s\n",cmd.network.AuthorizationType,cmd.network.Credentials)
if cmd.network.AuthorizationType == BASIC_AUTH && cmd.network.Credentials == "" {
cmd.getCredentialsFromCommandLine()
}

// now persist these credentials to the yaml file
cmd.config.SetNetworkCredentials(cmd.network.BrooklynUrl, cmd.network.Credentials)
@@ -206,13 +181,3 @@ func (cmd *Login) Run(scope scope.Scope, c *cli.Context) {
}
fmt.Printf("Connected to Brooklyn version %s at %s\n", loginVersion.Version, cmd.network.BrooklynUrl)
}

func (cmd *Login) isBasicAuth() bool {
return strings.EqualFold(cmd.network.AuthorizationType,BASIC_AUTH)
}

func (cmd *Login) parseAuthorizationParam(authParam string) []string {
return strings.SplitN(authParam, ":", 2)
}


@@ -250,7 +250,7 @@ func (config *Config) GetAuthType(target string) (authType string, err error){
authType, found = authTypeMap[target].(string)
}
// default behaviour
if! found{
if !found{
authType = "Basic"
}
return authType, err
@@ -26,22 +26,14 @@ import (

func TestConfig(t *testing.T) {

testFileFormat(t, "testconfig.json")
testFileFormat(t, "legacyConfig.json")
testFileFormat(t, "testconfig.json", true)
testFileFormat(t, "legacyConfig.json", false)

testAuthType(t, "testconfig.json", true)
testAuthType(t, "legacyConfig.json", false)
}

func mockHeaders() (expectedHeaders map[string]interface{}) {
expectedHeaders=make(map[string]interface{})
expectedHeaders["Header1"]="Header one"
expectedHeaders["Header2"]="Header 2"
expectedHeaders["header3"]=""
return
}


func getConfigFromFile(t *testing.T, testFile string)(config *Config) {
config = new(Config)
expectedTarget := "http://some.site:8081"
@@ -58,14 +50,16 @@ func getConfigFromFile(t *testing.T, testFile string)(config *Config) {
return
}

func testFileFormat(t *testing.T, testFile string) {
func testFileFormat(t *testing.T, testFile string, testBearer bool) {
config := getConfigFromFile(t, testFile)

_, credentials, err := config.GetNetworkCredentials()
assertCredentials(err, t, credentials, base64.StdEncoding.EncodeToString([]byte("user1:password1")))

credentials, err = config.GetNetworkCredentialsForTarget("http://another.one:8081")
assertCredentials(err, t, credentials, base64.StdEncoding.EncodeToString([]byte("user2"+":"+"password2")))
if testBearer{
credentials, err = config.GetNetworkCredentialsForTarget("http://another.one:8081")
assertCredentials(err, t, credentials, "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c")
}

}

@@ -1,7 +1,7 @@
{
"credentials": {
"http://some.site:8081": "dXNlcjE6cGFzc3dvcmQx",
"http://another.one:8081": "dXNlcjI6cGFzc3dvcmQy"
"http://another.one:8081": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
},
"authType": {
"http://another.one:8081": "Bearer"

0 comments on commit fe8d285

Please sign in to comment.