This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
175 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package admin | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
) | ||
|
||
// APIKey is a struct that represents the API key used to authenticate with the Admin service. | ||
type APIKey struct { | ||
ClientID string `json:"id"` | ||
Secret string `json:"secret"` | ||
} | ||
|
||
// Encode encodes the API key into a base64 encoded string. | ||
func (k APIKey) Encode() (string, error) { | ||
raw, err := json.Marshal(k) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString(raw), nil | ||
} | ||
|
||
// DecodeAPIKey decodes a base64 encoded API key into a struct. | ||
func DecodeAPIKey(apiKey string) (APIKey, error) { | ||
var k APIKey | ||
raw, err := base64.StdEncoding.DecodeString(apiKey) | ||
if err != nil { | ||
return k, err | ||
} | ||
|
||
err = json.Unmarshal(raw, &k) | ||
if err != nil { | ||
return k, err | ||
} | ||
|
||
return k, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package admin | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestDecodeAPIKey(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
apiKey string | ||
want APIKey | ||
wantErr assert.ErrorAssertionFunc | ||
}{ | ||
{"happy path", "eyJpZCI6IiIsInNlY3JldCI6IiJ9", APIKey{}, assert.NoError}, | ||
{"happy path", "eyJpZCI6ImFiYyIsInNlY3JldCI6IiJ9", APIKey{ClientID: "abc"}, assert.NoError}, | ||
{"happy path", "eyJpZCI6ImFiYyIsInNlY3JldCI6ImRlZiJ9", APIKey{ClientID: "abc", Secret: "def"}, assert.NoError}, | ||
{"happy path", "eyJpZCI6IiMkQCFAYWJjIiwic2VjcmV0IjoiIChcdTAwMjZAIGRlZiAifQ==", APIKey{ClientID: "#$@!@abc", Secret: " (&@ def "}, assert.NoError}, | ||
{"invalid base64", "blah bloh==", APIKey{}, assert.Error}, | ||
{"invalid json", "aGVsbG8gd29ybGQK", APIKey{}, assert.Error}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := DecodeAPIKey(tt.apiKey) | ||
if !tt.wantErr(t, err, fmt.Sprintf("DecodeAPIKey(%v)", tt.apiKey)) { | ||
return | ||
} | ||
assert.Equalf(t, tt.want, got, "DecodeAPIKey(%v)", tt.apiKey) | ||
}) | ||
} | ||
} | ||
|
||
func TestAPIKey_Encode(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
apiKey APIKey | ||
want string | ||
wantErr assert.ErrorAssertionFunc | ||
}{ | ||
{"happy path", APIKey{}, "eyJpZCI6IiIsInNlY3JldCI6IiJ9", assert.NoError}, | ||
{"happy path", APIKey{ClientID: "abc"}, "eyJpZCI6ImFiYyIsInNlY3JldCI6IiJ9", assert.NoError}, | ||
{"happy path", APIKey{ClientID: "abc", Secret: "def"}, "eyJpZCI6ImFiYyIsInNlY3JldCI6ImRlZiJ9", assert.NoError}, | ||
{"happy path", APIKey{ClientID: "#$@!@abc", Secret: " (&@ def "}, "eyJpZCI6IiMkQCFAYWJjIiwic2VjcmV0IjoiIChcdTAwMjZAIGRlZiAifQ==", assert.NoError}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
k := APIKey{ | ||
ClientID: tt.apiKey.ClientID, | ||
Secret: tt.apiKey.Secret, | ||
} | ||
got, err := k.Encode() | ||
if !tt.wantErr(t, err, "Encode()") { | ||
return | ||
} | ||
assert.Equalf(t, tt.want, got, "Encode()") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters