0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-29 18:21:10 +00:00
terraform-provider-proxmox/proxmox/api/token_auth.go
Pavel Boldyrev be6f220779
fix(vm): improve cpu.architecture handling (#1683)
* chore(tests): add option to select auth type (root user / token) for tests
* fix(vm): throw an error when `cpu.architecture` can't be set

---------

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
2024-12-17 22:16:45 -05:00

42 lines
1.0 KiB
Go

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package api
import (
"context"
"net/http"
"strings"
)
type tokenAuthenticator struct {
username string
token string
}
// NewTokenAuthenticator creates a new authenticator that uses a PVE API Token
// for authentication.
func NewTokenAuthenticator(toc TokenCredentials) (Authenticator, error) {
return &tokenAuthenticator{
username: strings.Split(toc.APIToken, "!")[0],
token: toc.APIToken,
}, nil
}
func (t *tokenAuthenticator) IsRoot(_ context.Context) bool {
return t.username == rootUsername
}
func (t *tokenAuthenticator) IsRootTicket(_ context.Context) bool {
// Logged using a token, therefore not a ticket login
return false
}
func (t *tokenAuthenticator) AuthenticateRequest(_ context.Context, req *http.Request) error {
req.Header.Set("Authorization", "PVEAPIToken="+t.token)
return nil
}