encode docker auth headers

pull/1/head
Brad Rydzewski 5 years ago
parent ef1504fb90
commit c23a30094d

@ -63,6 +63,21 @@ func ParseBytes(b []byte) ([]*drone.Registry, error) {
return Parse(bytes.NewReader(b))
}
// Encode returns the json marshaled, base64 encoded
// credential string that can be passed to the docker
// registry authentication header.
func Encode(username, password string) string {
v := struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}{
Username: username,
Password: password,
}
buf, _ := json.Marshal(&v)
return base64.URLEncoding.EncodeToString(buf)
}
// encode returns the encoded credentials.
func encode(username, password string) string {
return base64.StdEncoding.EncodeToString(

@ -5,6 +5,8 @@
package auths
import (
"bytes"
"encoding/base64"
"os"
"testing"
@ -101,6 +103,21 @@ func TestDecodeInvalid(t *testing.T) {
}
}
func TestEncode(t *testing.T) {
username := "octocat"
password := "correct-horse-battery-staple"
result := Encode(username, password)
got, err := base64.URLEncoding.DecodeString(result)
if err != nil {
t.Error(err)
return
}
want := []byte(`{"username":"octocat","password":"correct-horse-battery-staple"}`)
if bytes.Equal(got, want) == false {
t.Errorf("Could not decode credential header")
}
}
var sample = `{
"auths": {
"https://index.docker.io/v1/": {

Loading…
Cancel
Save