go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/service/user/interface.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 user
    16  
    17  import (
    18  	"context"
    19  )
    20  
    21  // RawInterface provides access to the "appengine/users" API methods.
    22  type RawInterface interface {
    23  	Current() *User
    24  	CurrentOAuth(scopes ...string) (*User, error)
    25  
    26  	IsAdmin() bool
    27  
    28  	LoginURL(dest string) (string, error)
    29  	LoginURLFederated(dest, identity string) (string, error)
    30  	LogoutURL(dest string) (string, error)
    31  
    32  	OAuthConsumerKey() (string, error)
    33  
    34  	// If this implementation supports it, this will return an instance of the
    35  	// Testable object for this service, which will let you 'log in' virtual users
    36  	// in your test cases. If the implementation doesn't support it, it will
    37  	// return nil.
    38  	GetTestable() Testable
    39  }
    40  
    41  // Current returns the currently logged-in user, or nil if the user is not
    42  // signed in.
    43  func Current(c context.Context) *User {
    44  	return Raw(c).Current()
    45  }
    46  
    47  // CurrentOAuth returns the user associated with the OAuth consumer making this
    48  // request.
    49  //
    50  // If the OAuth consumer did not make a valid OAuth request, or the scopes is
    51  // non-empty and the current user does not have at least one of the scopes, this
    52  // method will return an error.
    53  func CurrentOAuth(c context.Context, scopes ...string) (*User, error) {
    54  	return Raw(c).CurrentOAuth(scopes...)
    55  }
    56  
    57  // IsAdmin returns true if the current user is an administrator for this
    58  // AppEngine project.
    59  func IsAdmin(c context.Context) bool {
    60  	return Raw(c).IsAdmin()
    61  }
    62  
    63  // LoginURL returns a URL that, when visited, prompts the user to sign in, then
    64  // redirects the user to the URL specified by dest.
    65  func LoginURL(c context.Context, dest string) (string, error) {
    66  	return Raw(c).LoginURL(dest)
    67  }
    68  
    69  // LoginURLFederated is like LoginURL but accepts a user's OpenID identifier.
    70  func LoginURLFederated(c context.Context, dest, identity string) (string, error) {
    71  	return Raw(c).LoginURLFederated(dest, identity)
    72  }
    73  
    74  // LogoutURL returns a URL that, when visited, signs the user out, then redirects
    75  // the user to the URL specified by dest.
    76  func LogoutURL(c context.Context, dest string) (string, error) {
    77  	return Raw(c).LogoutURL(dest)
    78  }
    79  
    80  // OAuthConsumerKey returns the OAuth consumer key provided with the current
    81  // request.
    82  //
    83  // This method will return an error if the OAuth request was invalid.
    84  func OAuthConsumerKey(c context.Context) (string, error) {
    85  	return Raw(c).OAuthConsumerKey()
    86  }
    87  
    88  // GetTestable returns a Testable for the current task queue service in c, or
    89  // nil if it does not offer one.
    90  //
    91  // The Testable instance will let you 'log in' virtual users in your test cases.
    92  func GetTestable(c context.Context) Testable {
    93  	return Raw(c).GetTestable()
    94  }