Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Helper Function for creating new Basic Token Factory #59

Merged
merged 2 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
- added helper function for building basic auth map [#59](https://github.com/xmidt-org/bascule/pull/59)

## [v0.8.1]
- fixed data race in RemoteBearerTokenAcquirer [#55](https://github.com/xmidt-org/bascule/pull/55)
Expand Down
33 changes: 33 additions & 0 deletions basculehttp/tokenFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/base64"
"errors"
"fmt"
"net/http"

jwt "github.com/dgrijalva/jwt-go"
Expand Down Expand Up @@ -74,6 +75,38 @@ func (btf BasicTokenFactory) ParseAndValidate(ctx context.Context, _ *http.Reque
return bascule.NewToken("basic", principal, bascule.NewAttributes()), nil
}

// NewBasicTokenFactoryFromList takes a list of base64 encoded basic auth keys,
// decodes them, and supplies that list in map form of username to password.
// If a username is encoded in two different auth keys, it will be overwritten
// by the last occurence of that username with a password. If anoth
func NewBasicTokenFactoryFromList(encodedBasicAuthKeys []string) (BasicTokenFactory, error) {
kristinapathak marked this conversation as resolved.
Show resolved Hide resolved
btf := make(BasicTokenFactory)
errs := bascule.Errors{}

for _, encodedKey := range encodedBasicAuthKeys {
decoded, err := base64.StdEncoding.DecodeString(encodedKey)
if err != nil {
errs = append(errs, emperror.Wrap(err, fmt.Sprintf("failed to base64-decode basic auth key [%v]", encodedKey)))
continue
}

i := bytes.IndexByte(decoded, ':')
if i <= 0 {
errs = append(errs, fmt.Errorf("basic auth key [%v] is malformed", encodedKey))
continue
}

btf[string(decoded[:i])] = string(decoded[i+1:])
}

if len(errs) != 0 {
return btf, errs
}

// explicitly return nil so we don't have any empty error lists being returned.
return btf, nil
}

// BearerTokenFactory parses and does basic validation for a JWT token.
type BearerTokenFactory struct {
DefaultKeyId string
Expand Down
54 changes: 54 additions & 0 deletions basculehttp/tokenFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,60 @@ func TestBasicTokenFactory(t *testing.T) {
}
}

func TestNewBasicTokenFactoryFromList(t *testing.T) {
goodKey := `dXNlcjpwYXNz`
badKeyDecode := `dXNlcjpwYXN\\\`
badKeyNoColon := `dXNlcnBhc3M=`
goodMap := map[string]string{"user": "pass"}
emptyMap := map[string]string{}

tests := []struct {
description string
keyList []string
expectedDecodedMap BasicTokenFactory
expectedErr error
}{
{
description: "Success",
keyList: []string{goodKey},
expectedDecodedMap: goodMap,
},
{
description: "Success With Errors",
keyList: []string{goodKey, badKeyDecode, badKeyNoColon},
expectedDecodedMap: goodMap,
expectedErr: errors.New("multiple errors"),
},
{
description: "Decode Error",
keyList: []string{badKeyDecode},
expectedDecodedMap: emptyMap,
expectedErr: errors.New("failed to base64-decode basic auth key"),
},
{
description: "Success",
keyList: []string{badKeyNoColon},
expectedDecodedMap: emptyMap,
expectedErr: errors.New("malformed"),
},
}

for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
m, err := NewBasicTokenFactoryFromList(tc.keyList)
assert.Equal(tc.expectedDecodedMap, m)
if tc.expectedErr == nil || err == nil {
assert.Equal(tc.expectedErr, err)
} else {
assert.Contains(err.Error(), tc.expectedErr.Error())
}
})
}

}

//TODO: fix this test
// func TestBearerTokenFactory(t *testing.T) {
// parseFailErr := errors.New("parse fail test")
// resolveFailErr := errors.New("resolve fail test")
Expand Down