0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-01 11:02:59 +00:00
terraform-provider-proxmox/proxmox/api/token_auth.go
Pavel Boldyrev ab54aa1092
feat(core): Add API Token authentication (#350)
* feat(core): Add API Token authentication

* fix linter errrors

* update docs
2023-05-29 21:56:21 -04:00

36 lines
828 B
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 (
"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(token string) (Authenticator, error) {
return &tokenAuthenticator{
username: strings.Split(token, "!")[0],
token: token,
}, nil
}
func (t *tokenAuthenticator) IsRoot() bool {
return t.username == rootUsername
}
func (t *tokenAuthenticator) AuthenticateRequest(req *http.Request) error {
req.Header.Set("Authorization", "PVEAPIToken="+t.token)
return nil
}