2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-01-27 00:08:52 +00:00
|
|
|
package okta
|
|
|
|
|
|
|
|
import (
|
2022-05-12 00:09:29 +00:00
|
|
|
"encoding/json"
|
2017-01-27 00:08:52 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2022-05-12 00:09:29 +00:00
|
|
|
"time"
|
2017-01-27 00:08:52 +00:00
|
|
|
|
2022-05-12 00:09:29 +00:00
|
|
|
"github.com/hashicorp/go-secure-stdlib/base62"
|
2021-07-16 00:17:31 +00:00
|
|
|
pwd "github.com/hashicorp/go-secure-stdlib/password"
|
2017-01-27 00:08:52 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CLIHandler struct
|
|
|
|
type CLIHandler struct{}
|
|
|
|
|
|
|
|
// Auth cli method
|
2017-08-31 20:57:00 +00:00
|
|
|
func (h *CLIHandler) Auth(c *api.Client, m map[string]string) (*api.Secret, error) {
|
2017-01-27 00:08:52 +00:00
|
|
|
mount, ok := m["mount"]
|
|
|
|
if !ok {
|
|
|
|
mount = "okta"
|
|
|
|
}
|
|
|
|
|
|
|
|
username, ok := m["username"]
|
|
|
|
if !ok {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, fmt.Errorf("'username' var must be set")
|
2017-01-27 00:08:52 +00:00
|
|
|
}
|
|
|
|
password, ok := m["password"]
|
|
|
|
if !ok {
|
2018-01-18 17:14:19 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "Password (will be hidden): ")
|
2017-01-27 00:08:52 +00:00
|
|
|
var err error
|
|
|
|
password, err = pwd.Read(os.Stdin)
|
2018-01-18 17:14:19 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "\n")
|
2017-01-27 00:08:52 +00:00
|
|
|
if err != nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, err
|
2017-01-27 00:08:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"password": password,
|
|
|
|
}
|
|
|
|
|
2022-04-14 12:37:04 +00:00
|
|
|
// Okta or Google totp code
|
2021-02-22 05:18:17 +00:00
|
|
|
if totp, ok := m["totp"]; ok {
|
|
|
|
data["totp"] = totp
|
|
|
|
}
|
|
|
|
|
2022-04-14 12:37:04 +00:00
|
|
|
// provider is an optional parameter
|
|
|
|
if provider, ok := m["provider"]; ok {
|
|
|
|
data["provider"] = provider
|
|
|
|
}
|
|
|
|
|
2022-05-12 00:09:29 +00:00
|
|
|
nonce := base62.MustRandom(20)
|
|
|
|
data["nonce"] = nonce
|
|
|
|
|
|
|
|
// Create a done channel to signal termination of the login so that we can
|
|
|
|
// clean up the goroutine
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
defer close(doneCh)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2023-02-06 16:49:01 +00:00
|
|
|
timer := time.NewTimer(time.Second)
|
2022-05-12 00:09:29 +00:00
|
|
|
select {
|
|
|
|
case <-doneCh:
|
2023-02-06 16:49:01 +00:00
|
|
|
timer.Stop()
|
2022-05-12 00:09:29 +00:00
|
|
|
return
|
2023-02-06 16:49:01 +00:00
|
|
|
case <-timer.C:
|
2022-05-12 00:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, _ := c.Logical().Read(fmt.Sprintf("auth/%s/verify/%s", mount, nonce))
|
|
|
|
if resp != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "In Okta Verify, tap the number %q\n", resp.Data["correct_answer"].(json.Number))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-01-27 00:08:52 +00:00
|
|
|
path := fmt.Sprintf("auth/%s/login/%s", mount, username)
|
|
|
|
secret, err := c.Logical().Write(path, data)
|
|
|
|
if err != nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, err
|
2017-01-27 00:08:52 +00:00
|
|
|
}
|
|
|
|
if secret == nil {
|
2017-08-31 20:57:00 +00:00
|
|
|
return nil, fmt.Errorf("empty response from credential provider")
|
2017-01-27 00:08:52 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 20:57:00 +00:00
|
|
|
return secret, nil
|
2017-01-27 00:08:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Help method for okta cli
|
|
|
|
func (h *CLIHandler) Help() string {
|
|
|
|
help := `
|
2017-09-06 14:02:15 +00:00
|
|
|
Usage: vault login -method=okta [CONFIG K=V...]
|
2017-01-27 00:08:52 +00:00
|
|
|
|
2017-10-04 21:16:39 +00:00
|
|
|
The Okta auth method allows users to authenticate using Okta.
|
2017-01-27 00:08:52 +00:00
|
|
|
|
2017-09-02 22:50:21 +00:00
|
|
|
Authenticate as "sally":
|
|
|
|
|
2017-09-06 14:02:15 +00:00
|
|
|
$ vault login -method=okta username=sally
|
2017-09-02 22:50:21 +00:00
|
|
|
Password (will be hidden):
|
|
|
|
|
|
|
|
Authenticate as "bob":
|
|
|
|
|
2017-09-06 14:02:15 +00:00
|
|
|
$ vault login -method=okta username=bob password=password
|
2017-09-02 22:50:21 +00:00
|
|
|
|
|
|
|
Configuration:
|
|
|
|
|
|
|
|
password=<string>
|
2017-10-04 21:16:39 +00:00
|
|
|
Okta password to use for authentication. If not provided, the CLI will
|
2017-09-02 22:50:21 +00:00
|
|
|
prompt for this on stdin.
|
|
|
|
|
|
|
|
username=<string>
|
2017-10-04 21:16:39 +00:00
|
|
|
Okta username to use for authentication.
|
2017-09-06 14:02:15 +00:00
|
|
|
`
|
2017-01-27 00:08:52 +00:00
|
|
|
|
|
|
|
return strings.TrimSpace(help)
|
|
|
|
}
|