1package api
2
3import (
4	"context"
5	"errors"
6	"fmt"
7
8	"github.com/mitchellh/mapstructure"
9)
10
11func (c *Sys) ListAuth() (map[string]*AuthMount, error) {
12	r := c.c.NewRequest("GET", "/v1/sys/auth")
13
14	ctx, cancelFunc := context.WithCancel(context.Background())
15	defer cancelFunc()
16	resp, err := c.c.RawRequestWithContext(ctx, r)
17	if err != nil {
18		return nil, err
19	}
20	defer resp.Body.Close()
21
22	secret, err := ParseSecret(resp.Body)
23	if err != nil {
24		return nil, err
25	}
26	if secret == nil || secret.Data == nil {
27		return nil, errors.New("data from server response is empty")
28	}
29
30	mounts := map[string]*AuthMount{}
31	err = mapstructure.Decode(secret.Data, &mounts)
32	if err != nil {
33		return nil, err
34	}
35
36	return mounts, nil
37}
38
39// DEPRECATED: Use EnableAuthWithOptions instead
40func (c *Sys) EnableAuth(path, authType, desc string) error {
41	return c.EnableAuthWithOptions(path, &EnableAuthOptions{
42		Type:        authType,
43		Description: desc,
44	})
45}
46
47func (c *Sys) EnableAuthWithOptions(path string, options *EnableAuthOptions) error {
48	r := c.c.NewRequest("POST", fmt.Sprintf("/v1/sys/auth/%s", path))
49	if err := r.SetJSONBody(options); err != nil {
50		return err
51	}
52
53	ctx, cancelFunc := context.WithCancel(context.Background())
54	defer cancelFunc()
55	resp, err := c.c.RawRequestWithContext(ctx, r)
56	if err != nil {
57		return err
58	}
59	defer resp.Body.Close()
60
61	return nil
62}
63
64func (c *Sys) DisableAuth(path string) error {
65	r := c.c.NewRequest("DELETE", fmt.Sprintf("/v1/sys/auth/%s", path))
66
67	ctx, cancelFunc := context.WithCancel(context.Background())
68	defer cancelFunc()
69	resp, err := c.c.RawRequestWithContext(ctx, r)
70	if err == nil {
71		defer resp.Body.Close()
72	}
73	return err
74}
75
76// Rather than duplicate, we can use modern Go's type aliasing
77type EnableAuthOptions = MountInput
78type AuthConfigInput = MountConfigInput
79type AuthMount = MountOutput
80type AuthConfigOutput = MountConfigOutput
81