-
Notifications
You must be signed in to change notification settings - Fork 137
/
register.go
136 lines (124 loc) · 3.79 KB
/
register.go
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
package auth
import (
"encoding/json"
"errors"
"net/http"
"strings"
"github.com/cozy/cozy-stack/model/oauth"
"github.com/cozy/cozy-stack/pkg/config/config"
"github.com/cozy/cozy-stack/pkg/consts"
"github.com/cozy/cozy-stack/pkg/couchdb"
"github.com/cozy/cozy-stack/pkg/limits"
"github.com/cozy/cozy-stack/web/middlewares"
"github.com/labstack/echo/v4"
)
func registerClient(c echo.Context) error {
instance := middlewares.GetInstance(c)
err := config.GetRateLimiter().CheckRateLimit(instance, limits.OAuthClientType)
if limits.IsLimitReachedOrExceeded(err) {
return echo.NewHTTPError(http.StatusNotFound, "Not found")
}
client := new(oauth.Client)
if err := json.NewDecoder(c.Request().Body).Decode(client); err != nil {
return err
}
// We do not allow the creation of clients allowed to have an empty scope
// ("login" scope), except via the CLI.
if client.AllowLoginScope {
if _, ok := middlewares.GetCLIPermission(c); !ok {
return echo.NewHTTPError(http.StatusUnauthorized,
"Not authorized to create client with given parameters")
}
}
if err := client.Create(instance); err != nil {
return c.JSON(err.Code, err)
}
return c.JSON(http.StatusCreated, client)
}
func readClient(c echo.Context) error {
client := c.Get("client").(*oauth.Client)
client.TransformIDAndRev()
return c.JSON(http.StatusOK, client)
}
func updateClient(c echo.Context) error {
instance := middlewares.GetInstance(c)
err := config.GetRateLimiter().CheckRateLimit(instance, limits.OAuthClientType)
if limits.IsLimitReachedOrExceeded(err) {
return echo.NewHTTPError(http.StatusNotFound, "Not found")
}
clientID := c.Param("client-id")
defer LockOAuthClient(instance, clientID)()
oldClient, err := oauth.FindClient(instance, clientID)
if err != nil {
return c.JSON(http.StatusNotFound, echo.Map{
"error": "Client not found",
})
}
if err := checkClientToken(c, oldClient); err != nil {
return c.JSON(http.StatusUnauthorized, echo.Map{
"error": err.Error(),
})
}
client := new(oauth.Client)
if err := json.NewDecoder(c.Request().Body).Decode(client); err != nil {
return err
}
if err := client.Update(instance, oldClient); err != nil {
return c.JSON(err.Code, err)
}
return c.JSON(http.StatusOK, client)
}
func deleteClient(c echo.Context) error {
instance := middlewares.GetInstance(c)
clientID := c.Param("client-id")
defer LockOAuthClient(instance, clientID)()
client, err := oauth.FindClient(instance, clientID)
if err != nil {
if couchdb.IsNotFoundError(err) {
return c.NoContent(http.StatusNoContent)
}
return c.JSON(http.StatusInternalServerError, echo.Map{
"error": err.Error(),
})
}
if err := checkClientToken(c, client); err != nil {
return c.JSON(http.StatusUnauthorized, echo.Map{
"error": err.Error(),
})
}
if err := client.Delete(instance); err != nil {
return c.JSON(err.Code, err)
}
return c.NoContent(http.StatusNoContent)
}
func checkRegistrationToken(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
instance := middlewares.GetInstance(c)
client, err := oauth.FindClient(instance, c.Param("client-id"))
if err != nil {
return c.JSON(http.StatusNotFound, echo.Map{
"error": "Client not found",
})
}
if err := checkClientToken(c, client); err != nil {
return c.JSON(http.StatusUnauthorized, echo.Map{
"error": err.Error(),
})
}
c.Set("client", client)
return next(c)
}
}
func checkClientToken(c echo.Context, client *oauth.Client) error {
header := c.Request().Header.Get(echo.HeaderAuthorization)
if !strings.HasPrefix(header, "Bearer ") {
return errors.New("invalid_token")
}
token := header[len("Bearer "):]
instance := middlewares.GetInstance(c)
_, ok := client.ValidToken(instance, consts.RegistrationTokenAudience, token)
if !ok {
return errors.New("invalid_token")
}
return nil
}