go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/appengine/gaeauth/server/internal/authdbimpl/helpers.go (about) 1 // Copyright 2015 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package authdbimpl 16 17 import ( 18 "context" 19 20 "go.chromium.org/luci/gae/service/info" 21 "go.chromium.org/luci/server/auth/service" 22 ) 23 24 // authService is interface for service.AuthService. 25 // 26 // Unit tests inject fake implementation into the testing context. 27 type authService interface { 28 EnsureSubscription(ctx context.Context, subscription, pushURL string) error 29 DeleteSubscription(ctx context.Context, subscription string) error 30 PullPubSub(ctx context.Context, subscription string) (*service.Notification, error) 31 ProcessPubSubPush(ctx context.Context, body []byte) (*service.Notification, error) 32 GetLatestSnapshotRevision(ctx context.Context) (int64, error) 33 GetSnapshot(ctx context.Context, rev int64) (*service.Snapshot, error) 34 } 35 36 type contextKey int 37 38 // setAuthService injects authService implementation into the context. 39 // 40 // Used in unit tests. 41 func setAuthService(ctx context.Context, s authService) context.Context { 42 return context.WithValue(ctx, contextKey(0), s) 43 } 44 45 // getAuthService returns authService implementation injected into the context 46 // via setAuthService or *service.AuthService otherwise. 47 func getAuthService(ctx context.Context, url string) authService { 48 if s, _ := ctx.Value(contextKey(0)).(authService); s != nil { 49 return s 50 } 51 return &service.AuthService{URL: url} 52 } 53 54 // defaultNS returns GAE context configured to use default namespace. 55 // 56 // All publicly callable functions must use it to switch to default namespace. 57 // All internal functions expect the context to be in the default namespace. 58 // 59 // Idempotent. 60 func defaultNS(ctx context.Context) context.Context { 61 return info.MustNamespace(ctx, "") 62 }