go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/auth/openid/user.go (about)

     1  // Copyright 2020 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 openid
    16  
    17  import (
    18  	"context"
    19  	"strings"
    20  
    21  	"go.chromium.org/luci/auth/identity"
    22  	"go.chromium.org/luci/common/errors"
    23  
    24  	"go.chromium.org/luci/server/auth"
    25  )
    26  
    27  // UserFromIDToken validates the ID token and extracts user information from it.
    28  //
    29  // Returns the partially validated token and auth.User extracted from it.
    30  //
    31  // The caller is still responsible to verify token's Audience field.
    32  func UserFromIDToken(ctx context.Context, token string, discovery *DiscoveryDoc) (*IDToken, *auth.User, error) {
    33  	// Validate the discovery doc has necessary fields to proceed.
    34  	switch {
    35  	case discovery.Issuer == "":
    36  		return nil, nil, errors.Reason("openid: bad discovery doc, empty issuer").Err()
    37  	case discovery.JwksURI == "":
    38  		return nil, nil, errors.Reason("openid: bad discovery doc, empty jwks_uri").Err()
    39  	}
    40  
    41  	// Grab the signing keys needed to verify the token. This is almost always
    42  	// hitting the local process cache and thus must be fast.
    43  	signingKeys, err := discovery.SigningKeys(ctx)
    44  	if err != nil {
    45  		return nil, nil, err
    46  	}
    47  
    48  	// Unpack the ID token to grab the user information from it.
    49  	verifiedToken, err := VerifyIDToken(ctx, token, signingKeys, discovery.Issuer)
    50  	if err != nil {
    51  		return nil, nil, err
    52  	}
    53  
    54  	// Ignore non https:// URLs for pictures. We serve all pages over HTTPS and
    55  	// don't want to break this rule just for a pretty picture.
    56  	picture := verifiedToken.Picture
    57  	if picture != "" && !strings.HasPrefix(picture, "https://") {
    58  		picture = ""
    59  	}
    60  
    61  	// Build the identity string from the email. This essentially validates it
    62  	// against a regexp.
    63  	id, err := identity.MakeIdentity("user:" + verifiedToken.Email)
    64  	if err != nil {
    65  		return nil, nil, err
    66  	}
    67  
    68  	return verifiedToken, &auth.User{
    69  		Identity: id,
    70  		Email:    verifiedToken.Email,
    71  		Name:     verifiedToken.Name,
    72  		Picture:  picture,
    73  	}, nil
    74  }