go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/filter/featureBreaker/user.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 featureBreaker
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.chromium.org/luci/gae/service/user"
    21  )
    22  
    23  type userState struct {
    24  	*state
    25  
    26  	c context.Context
    27  	user.RawInterface
    28  }
    29  
    30  var _ user.RawInterface = (*userState)(nil)
    31  
    32  func (u *userState) CurrentOAuth(scopes ...string) (ret *user.User, err error) {
    33  	err = u.run(u.c, func() (err error) {
    34  		ret, err = u.RawInterface.CurrentOAuth(scopes...)
    35  		return
    36  	})
    37  	return
    38  }
    39  
    40  func (u *userState) LoginURL(dest string) (ret string, err error) {
    41  	err = u.run(u.c, func() (err error) {
    42  		ret, err = u.RawInterface.LoginURL(dest)
    43  		return
    44  	})
    45  	return
    46  }
    47  
    48  func (u *userState) LoginURLFederated(dest, identity string) (ret string, err error) {
    49  	err = u.run(u.c, func() (err error) {
    50  		ret, err = u.RawInterface.LoginURLFederated(dest, identity)
    51  		return
    52  	})
    53  	return
    54  }
    55  
    56  func (u *userState) LogoutURL(dest string) (ret string, err error) {
    57  	err = u.run(u.c, func() (err error) {
    58  		ret, err = u.RawInterface.LogoutURL(dest)
    59  		return
    60  	})
    61  	return
    62  }
    63  
    64  func (u *userState) OAuthConsumerKey() (ret string, err error) {
    65  	err = u.run(u.c, func() (err error) {
    66  		ret, err = u.RawInterface.OAuthConsumerKey()
    67  		return
    68  	})
    69  	return
    70  }
    71  
    72  // FilterUser installs a featureBreaker user filter in the context.
    73  func FilterUser(c context.Context, defaultError error) (context.Context, FeatureBreaker) {
    74  	state := newState(defaultError)
    75  	return user.AddFilters(c, func(ic context.Context, i user.RawInterface) user.RawInterface {
    76  		return &userState{state, ic, i}
    77  	}), state
    78  }