File size: 2,712 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package graphqlapp

import (
	"context"

	"github.com/target/goalert/apikey"
	"github.com/target/goalert/graphql2"
	"github.com/target/goalert/permission"
	"github.com/target/goalert/user"
)

type GQLAPIKey App

func (a *App) GQLAPIKey() graphql2.GQLAPIKeyResolver { return (*GQLAPIKey)(a) }

func (a *GQLAPIKey) CreatedBy(ctx context.Context, obj *graphql2.GQLAPIKey) (*user.User, error) {
	if obj.CreatedBy == nil {
		return nil, nil
	}

	return (*App)(a).FindOneUser(ctx, obj.CreatedBy.ID)
}

func (a *GQLAPIKey) UpdatedBy(ctx context.Context, obj *graphql2.GQLAPIKey) (*user.User, error) {
	if obj.UpdatedBy == nil {
		return nil, nil
	}

	return (*App)(a).FindOneUser(ctx, obj.UpdatedBy.ID)
}

func (q *Query) GqlAPIKeys(ctx context.Context) ([]graphql2.GQLAPIKey, error) {
	err := permission.LimitCheckAny(ctx, permission.Admin)
	if err != nil {
		return nil, err
	}

	keys, err := q.APIKeyStore.FindAllAdminGraphQLKeys(ctx)
	if err != nil {
		return nil, err
	}

	res := make([]graphql2.GQLAPIKey, len(keys))
	for i, k := range keys {
		res[i] = graphql2.GQLAPIKey{
			ID:          k.ID.String(),
			Name:        k.Name,
			Description: k.Description,
			CreatedAt:   k.CreatedAt,
			UpdatedAt:   k.UpdatedAt,
			ExpiresAt:   k.ExpiresAt,
			Query:       k.Query,
			Role:        graphql2.UserRole(k.Role),
		}

		if k.CreatedBy != nil {
			res[i].CreatedBy = &user.User{ID: k.CreatedBy.String()}
		}
		if k.UpdatedBy != nil {
			res[i].UpdatedBy = &user.User{ID: k.UpdatedBy.String()}
		}

		if k.LastUsed != nil {
			res[i].LastUsed = &graphql2.GQLAPIKeyUsage{
				Time: k.LastUsed.Time,
				Ua:   k.LastUsed.UserAgent,
				IP:   k.LastUsed.IP,
			}
		}
	}

	return res, nil
}

func (a *Mutation) UpdateGQLAPIKey(ctx context.Context, input graphql2.UpdateGQLAPIKeyInput) (bool, error) {
	id, err := parseUUID("ID", input.ID)
	if err != nil {
		return false, err
	}

	err = a.APIKeyStore.UpdateAdminGraphQLKey(ctx, id, input.Name, input.Description)
	return err == nil, err
}

func (a *Mutation) DeleteGQLAPIKey(ctx context.Context, input string) (bool, error) {
	id, err := parseUUID("ID", input)
	if err != nil {
		return false, err
	}

	err = a.APIKeyStore.DeleteAdminGraphQLKey(ctx, id)
	return err == nil, err
}

func (a *Mutation) CreateGQLAPIKey(ctx context.Context, input graphql2.CreateGQLAPIKeyInput) (*graphql2.CreatedGQLAPIKey, error) {
	id, tok, err := a.APIKeyStore.CreateAdminGraphQLKey(ctx, apikey.NewAdminGQLKeyOpts{
		Name:    input.Name,
		Desc:    input.Description,
		Expires: input.ExpiresAt,
		Query:   input.Query,
		Role:    permission.Role(input.Role),
	})
	if err != nil {
		return nil, err
	}

	return &graphql2.CreatedGQLAPIKey{
		ID:    id.String(),
		Token: tok,
	}, nil
}