go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/auth/authtest/method.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 authtest 16 17 import ( 18 "context" 19 "errors" 20 "net/url" 21 22 "go.chromium.org/luci/server/auth" 23 ) 24 25 // ErrAuthenticationError is returned by FakeAuth.Authenticate. 26 var ErrAuthenticationError = errors.New("authtest: fake Authenticate error") 27 28 // FakeAuth implements auth.Method's Authenticate by returning predefined 29 // user and session. 30 type FakeAuth struct { 31 User *auth.User // the user to return in Authenticate or nil for error 32 Session auth.Session // the session to return 33 Error error // an error to return from all methods, if set 34 } 35 36 // Authenticate returns predefined User object (if it is not nil) or error. 37 func (m FakeAuth) Authenticate(context.Context, auth.RequestMetadata) (*auth.User, auth.Session, error) { 38 if m.Error != nil { 39 return nil, nil, m.Error 40 } 41 if m.User == nil { 42 return nil, nil, ErrAuthenticationError 43 } 44 return m.User, m.Session, nil 45 } 46 47 // LoginURL returns fake login URL. 48 func (m FakeAuth) LoginURL(ctx context.Context, dest string) (string, error) { 49 if m.Error != nil { 50 return "", m.Error 51 } 52 return "http://fake.example.com/login?dest=" + url.QueryEscape(dest), nil 53 } 54 55 // LogoutURL returns fake logout URL. 56 func (m FakeAuth) LogoutURL(ctx context.Context, dest string) (string, error) { 57 if m.Error != nil { 58 return "", m.Error 59 } 60 return "http://fake.example.com/logout?dest=" + url.QueryEscape(dest), nil 61 }