blob: 243c0f8151760ae152e166756961bf5eeb3a6343 [file] [log] [blame]
// Copyright 2015 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package authtest
import (
"context"
"errors"
"net/http"
"net/url"
"go.chromium.org/luci/server/auth"
)
// ErrAuthenticationError is returned by FakeAuth.Authenticate.
var ErrAuthenticationError = errors.New("authtest: fake Authenticate error")
// FakeAuth implements auth.Method's Authenticate by returning predefined
// user and session.
type FakeAuth struct {
User *auth.User // the user to return in Authenticate or nil for error
Session auth.Session // the session to return
}
// Authenticate returns predefined User object (if it is not nil) or error.
func (m FakeAuth) Authenticate(context.Context, *http.Request) (*auth.User, auth.Session, error) {
if m.User == nil {
return nil, nil, ErrAuthenticationError
}
return m.User, m.Session, nil
}
// LoginURL returns fake login URL.
func (m FakeAuth) LoginURL(ctx context.Context, dest string) (string, error) {
return "http://fake.example.com/login?dest=" + url.QueryEscape(dest), nil
}
// LogoutURL returns fake logout URL.
func (m FakeAuth) LogoutURL(ctx context.Context, dest string) (string, error) {
return "http://fake.example.com/logout?dest=" + url.QueryEscape(dest), nil
}