-
Notifications
You must be signed in to change notification settings - Fork 299
/
identityserver.go
219 lines (196 loc) · 8.92 KB
/
identityserver.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package identityserver
import (
"context"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres" // Postgres database driver.
"go.thethings.network/lorawan-stack/v3/api"
"go.thethings.network/lorawan-stack/v3/pkg/auth/rights"
"go.thethings.network/lorawan-stack/v3/pkg/cluster"
"go.thethings.network/lorawan-stack/v3/pkg/component"
"go.thethings.network/lorawan-stack/v3/pkg/email"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"go.thethings.network/lorawan-stack/v3/pkg/identityserver/store"
"go.thethings.network/lorawan-stack/v3/pkg/log"
"go.thethings.network/lorawan-stack/v3/pkg/oauth"
"go.thethings.network/lorawan-stack/v3/pkg/redis"
"go.thethings.network/lorawan-stack/v3/pkg/rpcmiddleware/hooks"
"go.thethings.network/lorawan-stack/v3/pkg/rpcmiddleware/rpclog"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"google.golang.org/grpc"
)
// IdentityServer implements the Identity Server component.
//
// The Identity Server exposes the Registry and Access services for Applications,
// OAuth clients, Gateways, Organizations and Users.
type IdentityServer struct {
*component.Component
ctx context.Context
config *Config
db *gorm.DB
redis *redis.Client
emailTemplates *email.TemplateRegistry
oauth oauth.Server
api.UnimplementedIsServer
}
// Context returns the context of the Identity Server.
func (is *IdentityServer) Context() context.Context {
return is.ctx
}
// SetRedisCache configures the given redis instance for caching.
func (is *IdentityServer) SetRedisCache(redis *redis.Client) {
is.redis = redis
}
type ctxKeyType struct{}
var ctxKey ctxKeyType
func (is *IdentityServer) configFromContext(ctx context.Context) *Config {
if config, ok := ctx.Value(ctxKey).(*Config); ok {
return config
}
return is.config
}
var errDBNeedsMigration = errors.Define("db_needs_migration", "the database needs to be migrated")
// New returns new *IdentityServer.
func New(c *component.Component, config *Config) (is *IdentityServer, err error) {
is = &IdentityServer{
Component: c,
ctx: log.NewContextWithField(c.Context(), "namespace", "identityserver"),
config: config,
}
is.db, err = store.Open(is.Context(), is.config.DatabaseURI)
if err != nil {
return nil, err
}
if c.LogDebug() {
is.db = is.db.Debug()
}
if err = store.Check(is.db); err != nil {
return nil, errDBNeedsMigration.WithCause(err)
}
go func() {
<-is.Context().Done()
is.db.Close()
}()
is.emailTemplates, err = is.initEmailTemplates(is.Context())
if err != nil {
return nil, err
}
is.config.OAuth.CSRFAuthKey = is.GetBaseConfig(is.Context()).HTTP.Cookie.HashKey
is.oauth, err = oauth.NewServer(c, struct {
store.UserStore
store.UserSessionStore
store.ClientStore
store.OAuthStore
}{
UserStore: store.GetUserStore(is.db),
UserSessionStore: store.GetUserSessionStore(is.db),
ClientStore: store.GetClientStore(is.db),
OAuthStore: store.GetOAuthStore(is.db),
}, is.config.OAuth)
if err != nil {
return nil, err
}
c.AddContextFiller(func(ctx context.Context) context.Context {
ctx = is.withRequestAccessCache(ctx)
ctx = rights.NewContextWithFetcher(ctx, is)
ctx = rights.NewContextWithCache(ctx)
return ctx
})
for _, hook := range []struct {
name string
middleware hooks.UnaryHandlerMiddleware
}{
{rpclog.NamespaceHook, rpclog.UnaryNamespaceHook("identityserver")},
{cluster.HookName, c.ClusterAuthUnaryHook()},
} {
hooks.RegisterUnaryHook("/ttn.lorawan.v3.Is", hook.name, hook.middleware)
hooks.RegisterUnaryHook("/ttn.lorawan.v3.ApplicationRegistry", hook.name, hook.middleware)
hooks.RegisterUnaryHook("/ttn.lorawan.v3.ApplicationAccess", hook.name, hook.middleware)
hooks.RegisterUnaryHook("/ttn.lorawan.v3.ClientRegistry", hook.name, hook.middleware)
hooks.RegisterUnaryHook("/ttn.lorawan.v3.ClientAccess", hook.name, hook.middleware)
hooks.RegisterUnaryHook("/ttn.lorawan.v3.EndDeviceRegistry", hook.name, hook.middleware)
hooks.RegisterUnaryHook("/ttn.lorawan.v3.GatewayRegistry", hook.name, hook.middleware)
hooks.RegisterUnaryHook("/ttn.lorawan.v3.GatewayAccess", hook.name, hook.middleware)
hooks.RegisterUnaryHook("/ttn.lorawan.v3.OrganizationRegistry", hook.name, hook.middleware)
hooks.RegisterUnaryHook("/ttn.lorawan.v3.OrganizationAccess", hook.name, hook.middleware)
hooks.RegisterUnaryHook("/ttn.lorawan.v3.UserRegistry", hook.name, hook.middleware)
hooks.RegisterUnaryHook("/ttn.lorawan.v3.UserAccess", hook.name, hook.middleware)
}
hooks.RegisterUnaryHook("/ttn.lorawan.v3.EntityAccess", rpclog.NamespaceHook, rpclog.UnaryNamespaceHook("identityserver"))
hooks.RegisterUnaryHook("/ttn.lorawan.v3.EntityAccess", cluster.HookName, c.ClusterAuthUnaryHook())
hooks.RegisterUnaryHook("/ttn.lorawan.v3.OAuthAuthorizationRegistry", rpclog.NamespaceHook, rpclog.UnaryNamespaceHook("identityserver"))
c.RegisterGRPC(is)
c.RegisterWeb(is.oauth)
return is, nil
}
func (is *IdentityServer) withDatabase(ctx context.Context, f func(*gorm.DB) error) error {
return store.Transact(ctx, is.db, f)
}
// RegisterServices registers services provided by is at s.
func (is *IdentityServer) RegisterServices(s *grpc.Server) {
api.RegisterIsServer(s, is)
ttnpb.RegisterEntityAccessServer(s, &entityAccess{IdentityServer: is})
ttnpb.RegisterApplicationRegistryServer(s, &applicationRegistry{IdentityServer: is})
ttnpb.RegisterApplicationAccessServer(s, &applicationAccess{IdentityServer: is})
ttnpb.RegisterClientRegistryServer(s, &clientRegistry{IdentityServer: is})
ttnpb.RegisterClientAccessServer(s, &clientAccess{IdentityServer: is})
ttnpb.RegisterEndDeviceRegistryServer(s, &endDeviceRegistry{IdentityServer: is})
ttnpb.RegisterGatewayRegistryServer(s, &gatewayRegistry{IdentityServer: is})
ttnpb.RegisterGatewayAccessServer(s, &gatewayAccess{IdentityServer: is})
ttnpb.RegisterOrganizationRegistryServer(s, &organizationRegistry{IdentityServer: is})
ttnpb.RegisterOrganizationAccessServer(s, &organizationAccess{IdentityServer: is})
ttnpb.RegisterUserRegistryServer(s, &userRegistry{IdentityServer: is})
ttnpb.RegisterUserAccessServer(s, &userAccess{IdentityServer: is})
ttnpb.RegisterUserInvitationRegistryServer(s, &invitationRegistry{IdentityServer: is})
ttnpb.RegisterEntityRegistrySearchServer(s, ®istrySearch{IdentityServer: is})
ttnpb.RegisterEndDeviceRegistrySearchServer(s, ®istrySearch{IdentityServer: is})
ttnpb.RegisterOAuthAuthorizationRegistryServer(s, &oauthRegistry{IdentityServer: is})
ttnpb.RegisterContactInfoRegistryServer(s, &contactInfoRegistry{IdentityServer: is})
}
// RegisterHandlers registers gRPC handlers.
func (is *IdentityServer) RegisterHandlers(s *runtime.ServeMux, conn *grpc.ClientConn) {
ttnpb.RegisterIsHandler(is.Context(), s, conn)
ttnpb.RegisterEntityAccessHandler(is.Context(), s, conn)
ttnpb.RegisterApplicationRegistryHandler(is.Context(), s, conn)
ttnpb.RegisterApplicationAccessHandler(is.Context(), s, conn)
ttnpb.RegisterClientRegistryHandler(is.Context(), s, conn)
ttnpb.RegisterClientAccessHandler(is.Context(), s, conn)
ttnpb.RegisterEndDeviceRegistryHandler(is.Context(), s, conn)
ttnpb.RegisterGatewayRegistryHandler(is.Context(), s, conn)
ttnpb.RegisterGatewayAccessHandler(is.Context(), s, conn)
ttnpb.RegisterOrganizationRegistryHandler(is.Context(), s, conn)
ttnpb.RegisterOrganizationAccessHandler(is.Context(), s, conn)
ttnpb.RegisterUserRegistryHandler(is.Context(), s, conn)
ttnpb.RegisterUserAccessHandler(is.Context(), s, conn)
ttnpb.RegisterUserInvitationRegistryHandler(is.Context(), s, conn)
ttnpb.RegisterEntityRegistrySearchHandler(is.Context(), s, conn)
ttnpb.RegisterEndDeviceRegistrySearchHandler(is.Context(), s, conn)
ttnpb.RegisterOAuthAuthorizationRegistryHandler(is.Context(), s, conn)
ttnpb.RegisterContactInfoRegistryHandler(is.Context(), s, conn)
}
// Roles returns the roles that the Identity Server fulfills.
func (is *IdentityServer) Roles() []ttnpb.ClusterRole {
return []ttnpb.ClusterRole{ttnpb.ClusterRole_ACCESS, ttnpb.ClusterRole_ENTITY_REGISTRY}
}
func (is *IdentityServer) getMembershipStore(ctx context.Context, db *gorm.DB) store.MembershipStore {
s := store.GetMembershipStore(db)
if is.redis != nil {
if membershipTTL := is.configFromContext(ctx).AuthCache.MembershipTTL; membershipTTL > 0 {
s = store.GetMembershipCache(s, is.redis, membershipTTL)
}
}
return s
}