mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-27 09:21:14 -06:00
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:
parent
3c65b5dd61
commit
43a3357473
20
svchost/auth/token_credentials.go
Normal file
20
svchost/auth/token_credentials.go
Normal 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))
|
||||
}
|
18
svchost/auth/token_credentials_test.go
Normal file
18
svchost/auth/token_credentials_test.go
Normal 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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user