-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathservice.go
164 lines (141 loc) · 5.38 KB
/
service.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
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
package svc
import (
"net/http"
"time"
"github.com/owncloud/ocis/ocis-pkg/service/grpc"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/render"
accounts "github.com/owncloud/ocis/accounts/pkg/proto/v0"
"github.com/owncloud/ocis/ocis-pkg/account"
"github.com/owncloud/ocis/ocis-pkg/log"
opkgm "github.com/owncloud/ocis/ocis-pkg/middleware"
"github.com/owncloud/ocis/ocis-pkg/roles"
"github.com/owncloud/ocis/ocs/pkg/config"
ocsm "github.com/owncloud/ocis/ocs/pkg/middleware"
"github.com/owncloud/ocis/ocs/pkg/service/v0/data"
"github.com/owncloud/ocis/ocs/pkg/service/v0/response"
settings "github.com/owncloud/ocis/settings/pkg/proto/v0"
)
// Service defines the extension handlers.
type Service interface {
ServeHTTP(http.ResponseWriter, *http.Request)
GetConfig(http.ResponseWriter, *http.Request)
}
// NewService returns a service implementation for Service.
func NewService(opts ...Option) Service {
options := newOptions(opts...)
m := chi.NewMux()
m.Use(options.Middleware...)
roleService := options.RoleService
if roleService == nil {
roleService = settings.NewRoleService("com.owncloud.api.settings", grpc.DefaultClient)
}
roleManager := options.RoleManager
if roleManager == nil {
m := roles.NewManager(
roles.CacheSize(1024),
roles.CacheTTL(time.Hour*24*7),
roles.Logger(options.Logger),
roles.RoleService(roleService),
)
roleManager = &m
}
svc := Ocs{
config: options.Config,
mux: m,
RoleManager: roleManager,
logger: options.Logger,
}
requireUser := ocsm.RequireUser()
requireAdmin := ocsm.RequireAdmin(
ocsm.RoleManager(roleManager),
ocsm.Logger(options.Logger),
)
requireSelfOrAdmin := ocsm.RequireSelfOrAdmin(
ocsm.RoleManager(roleManager),
ocsm.Logger(options.Logger),
)
m.Route(options.Config.HTTP.Root, func(r chi.Router) {
r.NotFound(svc.NotFound)
r.Use(middleware.StripSlashes)
r.Use(opkgm.ExtractAccountUUID(
account.Logger(options.Logger),
account.JWTSecret(options.Config.TokenManager.JWTSecret)),
)
r.Use(ocsm.OCSFormatCtx) // updates request Accept header according to format=(json|xml) query parameter
r.Route("/v{version:(1|2)}.php", func(r chi.Router) {
r.Use(response.VersionCtx) // stores version in context
r.Route("/apps/files_sharing/api/v1", func(r chi.Router) {})
r.Route("/apps/notifications/api/v1", func(r chi.Router) {})
r.Route("/cloud", func(r chi.Router) {
r.Route("/capabilities", func(r chi.Router) {})
// TODO /apps
r.Route("/user", func(r chi.Router) {
r.With(requireSelfOrAdmin).Get("/", svc.GetSelf)
r.Get("/signing-key", svc.GetSigningKey)
})
// for /users endpoints see https://github.com/owncloud/core/blob/master/apps/provisioning_api/appinfo/routes.php#L44-L56
r.Route("/users", func(r chi.Router) {
r.With(requireAdmin).Get("/", svc.ListUsers)
r.With(requireAdmin).Post("/", svc.AddUser)
r.Route("/{userid}", func(r chi.Router) {
r.With(requireUser).Get("/", svc.GetUser)
r.With(requireSelfOrAdmin).Put("/", svc.EditUser)
r.With(requireAdmin).Delete("/", svc.DeleteUser)
r.With(requireAdmin).Put("/enable", svc.EnableUser)
r.With(requireAdmin).Put("/disable", svc.DisableUser)
})
r.Route("/{userid}/groups", func(r chi.Router) {
r.With(requireSelfOrAdmin).Get("/", svc.ListUserGroups)
r.With(requireAdmin).Post("/", svc.AddToGroup)
r.With(requireAdmin).Delete("/", svc.RemoveFromGroup)
})
r.Route("/{userid}/subadmins", func(r chi.Router) {
r.With(requireAdmin).Post("/", svc.NotImplementedStub)
r.With(requireSelfOrAdmin).Get("/", svc.NotImplementedStub)
r.With(requireAdmin).Delete("/", svc.NotImplementedStub)
})
})
// for /groups endpoints see https://github.com/owncloud/core/blob/master/apps/provisioning_api/appinfo/routes.php#L65-L69
r.Route("/groups", func(r chi.Router) {
r.With(requireAdmin).Get("/", svc.ListGroups)
r.With(requireAdmin).Post("/", svc.AddGroup)
r.With(requireSelfOrAdmin).Get("/{groupid}", svc.GetGroupMembers)
r.With(requireAdmin).Delete("/{groupid}", svc.DeleteGroup)
r.With(requireAdmin).Get("/{groupid}/subadmins", svc.NotImplementedStub)
})
})
r.Route("/config", func(r chi.Router) {
r.With(requireUser).Get("/", svc.GetConfig)
})
})
})
return svc
}
// Ocs defines implements the business logic for Service.
type Ocs struct {
config *config.Config
logger log.Logger
RoleService settings.RoleService
RoleManager *roles.Manager
mux *chi.Mux
}
// ServeHTTP implements the Service interface.
func (o Ocs) ServeHTTP(w http.ResponseWriter, r *http.Request) {
o.mux.ServeHTTP(w, r)
}
// NotFound uses ErrRender to always return a proper OCS payload
func (o Ocs) NotFound(w http.ResponseWriter, r *http.Request) {
mustNotFail(render.Render(w, r, response.ErrRender(data.MetaNotFound.StatusCode, "not found")))
}
func (o Ocs) getAccountService() accounts.AccountsService {
return accounts.NewAccountsService("com.owncloud.api.accounts", grpc.DefaultClient)
}
func (o Ocs) getGroupsService() accounts.GroupsService {
return accounts.NewGroupsService("com.owncloud.api.accounts", grpc.DefaultClient)
}
// NotImplementedStub returns a not implemented error
func (o Ocs) NotImplementedStub(w http.ResponseWriter, r *http.Request) {
mustNotFail(render.Render(w, r, response.ErrRender(data.MetaUnknownError.StatusCode, "Not implemented")))
}