svchost/auth: token-based HostCredentials

This is the only credentials type we support right now, which just sends
an opaque token via the "Bearer" HTTP auth scheme.
This commit is contained in:
Martin Atkins 2017-10-17 18:01:26 -07:00
parent 3c65b5dd61
commit 43a3357473
2 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package auth
import (
"net/http"
)
// HostCredentialsToken is a HostCredentials implementation that represents a
// single "bearer token", to be sent to the server via an Authorization header
// with the auth type set to "Bearer"
type HostCredentialsToken string
// PrepareRequest alters the given HTTP request by setting its Authorization
// header to the string "Bearer " followed by the encapsulated authentication
// token.
func (tc HostCredentialsToken) PrepareRequest(req *http.Request) {
if req.Header == nil {
req.Header = http.Header{}
}
req.Header.Set("Authorization", "Bearer "+string(tc))
}

View File

@ -0,0 +1,18 @@
package auth
import (
"net/http"
"testing"
)
func TestHostCredentialsToken(t *testing.T) {
creds := HostCredentialsToken("foo-bar")
req := &http.Request{}
creds.PrepareRequest(req)
authStr := req.Header.Get("authorization")
if got, want := authStr, "Bearer foo-bar"; got != want {
t.Errorf("wrong Authorization header value %q; want %q", got, want)
}
}