aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/api_user.go
blob: df667db13828bf75c6edb8305009da67950853a0 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package controllers

import (
	"context"
	"net/http"

	"code.crute.us/mcrute/cloud-identity-broker/app/models"

	glecho "code.crute.us/mcrute/golib/echo"
	"code.crute.us/mcrute/golib/echo/controller"
	"github.com/labstack/echo/v4"
)

type APIUserHandler struct {
	Store models.UserStore
}

func (h *APIUserHandler) Register(prefix string, r glecho.URLRouter, mw ...echo.MiddlewareFunc) {
	// This resource did not exist in the V1 API and thus has no V1
	// representation. We use the default handlers for V1 because otherwise
	// requests with V1 Accept headers would result in 406 Unacceptable errors.
	gh := &controller.ContentTypeNegotiatingHandler{
		DefaultHandler: h.HandleGet,
		Handlers: map[string]echo.HandlerFunc{
			contentTypeV1: h.HandleGet,
			contentTypeV2: h.HandleGet,
		},
	}
	r.GET(prefix, gh.Handle, mw...)

	ph := &controller.ContentTypeNegotiatingHandler{
		DefaultHandler: h.HandlePut,
		Handlers: map[string]echo.HandlerFunc{
			contentTypeV1: h.HandlePut,
			contentTypeV2: h.HandlePut,
		},
	}
	r.PUT(prefix, ph.Handle, mw...)

	poh := &controller.ContentTypeNegotiatingHandler{
		DefaultHandler: h.HandlePost,
		Handlers: map[string]echo.HandlerFunc{
			contentTypeV1: h.HandlePost,
			contentTypeV2: h.HandlePost,
		},
	}
	r.POST(prefix, poh.Handle, mw...)

	r.DELETE(prefix, h.HandleDelete, mw...)
}

func (h *APIUserHandler) HandleGet(c echo.Context) error {
	u, err := h.Store.Get(context.Background(), c.Param("user"))
	if err != nil {
		return echo.ErrInternalServerError
	}

	return c.JSON(http.StatusOK, u)
}

func validateKeysAndTokens(in *models.User) error {
	for k, v := range in.Keys {
		if k != v.KeyId {
			return &echo.HTTPError{
				Code:    http.StatusBadRequest,
				Message: "Key ID must match hash key.",
			}
		}

		if v.PrivateKey == nil && v.PublicKey == nil {
			return &echo.HTTPError{
				Code:    http.StatusBadRequest,
				Message: "One of public_key or private_key must be set",
			}
		}

		if v.PrivateKey != nil && v.PublicKey != nil {
			return &echo.HTTPError{
				Code:    http.StatusBadRequest,
				Message: "Only one of public_key or private_key may be set",
			}
		}
	}

	for k, v := range in.AuthTokens {
		if k != v.Kind {
			return &echo.HTTPError{
				Code:    http.StatusBadRequest,
				Message: "Token kind must match hash key.",
			}
		}
	}

	return nil
}

func (h *APIUserHandler) HandlePut(c echo.Context) error {
	var in models.User
	if err := c.Echo().JSONSerializer.Deserialize(c, &in); err != nil {
		return echo.ErrBadRequest
	}

	u, err := h.Store.Get(context.Background(), c.Param("user"))
	if err != nil {
		return echo.ErrInternalServerError
	}

	if in.Username != u.Username {
		return &echo.HTTPError{
			Code:    http.StatusBadRequest,
			Message: "Username can not be changed. Create a new user.",
		}
	}

	if in.Deleted != nil && u.Deleted == nil {
		return &echo.HTTPError{
			Code:    http.StatusBadRequest,
			Message: "Use the DELETE method to delete a record",
		}
	}

	if err := validateKeysAndTokens(&in); err != nil {
		return err
	}

	err = h.Store.Put(context.Background(), &in)
	if err != nil {
		return echo.ErrInternalServerError
	}

	return c.String(http.StatusNoContent, "")
}

func (h *APIUserHandler) HandlePost(c echo.Context) error {
	var in models.User
	if err := c.Echo().JSONSerializer.Deserialize(c, &in); err != nil {
		return echo.ErrBadRequest
	}

	_, err := h.Store.Get(context.Background(), c.Param("user"))
	if err == nil {
		return &echo.HTTPError{
			Code:    http.StatusConflict,
			Message: "User with username already exists.",
		}
	}

	if in.Deleted != nil {
		return &echo.HTTPError{
			Code:    http.StatusBadRequest,
			Message: "Can not create deleted user, set Deleted to null",
		}
	}

	if err := validateKeysAndTokens(&in); err != nil {
		return err
	}

	err = h.Store.Put(context.Background(), &in)
	if err != nil {
		return echo.ErrInternalServerError
	}

	c.Response().Header().Add("Location", glecho.URLFor(c, "/api/user", in.Username).String())

	return c.String(http.StatusCreated, "")
}

func (h *APIUserHandler) HandleDelete(c echo.Context) error {
	u, err := h.Store.Get(context.Background(), c.Param("user"))
	if err != nil {
		return echo.ErrInternalServerError
	}

	err = h.Store.Delete(context.Background(), u)
	if err != nil {
		return echo.ErrInternalServerError
	}

	return c.String(http.StatusNoContent, "")
}