go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/loginsessions/internal/client.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 internal
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.chromium.org/luci/common/logging"
    21  
    22  	"go.chromium.org/luci/server/auth"
    23  )
    24  
    25  // GoogleAuthorizationEndpoint is Google's authorization endpoint URL.
    26  const GoogleAuthorizationEndpoint = "https://accounts.google.com/o/oauth2/v2/auth"
    27  
    28  // OAuthClientProvider returns OAuth client details for known clients.
    29  //
    30  // Returns nil if the client is not known or an error if the check failed.
    31  type OAuthClientProvider func(ctx context.Context, clientID string) (*OAuthClient, error)
    32  
    33  // OAuthClient represents a known accepted OAuth client.
    34  type OAuthClient struct {
    35  	// ProviderName is the name of the identity provider shown on the web pages.
    36  	ProviderName string
    37  	// AuthorizationEndpoint is OAuth endpoint to redirect the user to.
    38  	AuthorizationEndpoint string
    39  }
    40  
    41  // AuthDBClientProvider checks if a client is registered in the AuthDB.
    42  func AuthDBClientProvider(ctx context.Context, clientID string) (*OAuthClient, error) {
    43  	logging.Infof(ctx, "Checking OAuth client ID: %s", clientID)
    44  	switch yes, err := auth.GetState(ctx).DB().IsAllowedOAuthClientID(ctx, "", clientID); {
    45  	case err != nil:
    46  		return nil, err
    47  	case yes:
    48  		// Only Google OAuth clients are supported currently.
    49  		return &OAuthClient{
    50  			ProviderName:          "Google Accounts",
    51  			AuthorizationEndpoint: GoogleAuthorizationEndpoint,
    52  		}, nil
    53  	default:
    54  		return nil, nil
    55  	}
    56  }