go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/auth_service/impl/servers/oauth/server.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 oauth contains methods to work with oauth endpoint. 16 package oauth 17 18 import ( 19 "encoding/json" 20 21 "google.golang.org/grpc/codes" 22 "google.golang.org/grpc/status" 23 24 "go.chromium.org/luci/common/errors" 25 "go.chromium.org/luci/gae/service/datastore" 26 "go.chromium.org/luci/server/router" 27 28 "go.chromium.org/luci/auth_service/impl/model" 29 ) 30 31 // HandleLegacyOAuthEndpoint returns client_id and client_secret to support 32 // legacy services that depend on the legacy oauth endpoint for OAuth2 login on a client. 33 // Returns client_id and client_secret to use for OAuth2 login on a client. 34 func HandleLegacyOAuthEndpoint(ctx *router.Context) error { 35 c, w := ctx.Request.Context(), ctx.Writer 36 var globalCfgEntity *model.AuthGlobalConfig 37 var replicationStateEntity *model.AuthReplicationState 38 var err error 39 40 switch globalCfgEntity, err = model.GetAuthGlobalConfig(c); { 41 case errors.Is(err, datastore.ErrNoSuchEntity): 42 errors.Log(c, err) 43 return status.Errorf(codes.Internal, "no Global Config entity found in datastore.") 44 case err != nil: 45 errors.Log(c, err) 46 return status.Errorf(codes.Internal, "something went wrong... see logs") 47 } 48 49 switch replicationStateEntity, err = model.GetReplicationState(c); { 50 case errors.Is(err, datastore.ErrNoSuchEntity): 51 errors.Log(c, err) 52 return status.Errorf(codes.Internal, "no Replication State entity found in datastore.") 53 case err != nil: 54 errors.Log(c, err) 55 return status.Errorf(codes.Internal, "something went wrong... see logs") 56 } 57 58 blob, err := json.Marshal(map[string]any{ 59 "token_server_url": globalCfgEntity.TokenServerURL, 60 "client_not_so_secret": globalCfgEntity.OAuthClientSecret, 61 "additional_client_ids": globalCfgEntity.OAuthAdditionalClientIDs, 62 "client_id": globalCfgEntity.OAuthClientID, 63 "primary_url": replicationStateEntity.PrimaryURL, 64 }) 65 66 if err != nil { 67 return err 68 } 69 70 if _, err := w.Write(blob); err != nil { 71 return err 72 } 73 74 return nil 75 }