go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/luci_notify/common/appid.go (about) 1 // Copyright 2022 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 common includes any common utility functions that are used from 16 // multiple other packages in the project. 17 package common 18 19 import ( 20 "context" 21 22 "go.chromium.org/luci/common/errors" 23 "go.chromium.org/luci/server/auth" 24 "go.chromium.org/luci/server/auth/signing" 25 "go.chromium.org/luci/server/auth/signing/signingtest" 26 ) 27 28 // GetAppID returns the App ID for this service, or an error if we fail to get 29 // it. It should only ever fail if the context isn't configured properly. 30 func GetAppID(ctx context.Context) (string, error) { 31 signer := auth.GetSigner(ctx) 32 if signer == nil { 33 return "", errors.New("failed to get the Signer instance for the service") 34 } 35 36 info, err := signer.ServiceInfo(ctx) 37 if err != nil { 38 return "", errors.Annotate(err, "failed to get service info").Err() 39 } 40 41 return info.AppID, nil 42 } 43 44 // SetAppIDForTest installs a test Signer implementation into the context, with 45 // the given app ID. 46 func SetAppIDForTest(c context.Context, appID string) context.Context { 47 return auth.ModifyConfig(c, func(cfg auth.Config) auth.Config { 48 cfg.Signer = signingtest.NewSigner(&signing.ServiceInfo{ 49 AppID: appID, 50 }) 51 return cfg 52 }) 53 }