bb5d6269 |
package session
import (
"errors"
"net/http"
|
e2ffc795 |
"github.com/GoogleCloudPlatform/kubernetes/pkg/auth/user" |
bb5d6269 |
)
const UserNameKey = "user.name" |
66c42785 |
const UserUIDKey = "user.uid" |
bb5d6269 |
|
974ac676 |
type Authenticator struct { |
bb5d6269 |
store Store
name string
}
|
974ac676 |
func NewAuthenticator(store Store, name string) *Authenticator {
return &Authenticator{ |
bb5d6269 |
store: store,
name: name,
}
}
|
e2ffc795 |
func (a *Authenticator) AuthenticateRequest(req *http.Request) (user.Info, bool, error) { |
bb5d6269 |
session, err := a.store.Get(req, a.name)
if err != nil {
return nil, false, err
} |
66c42785 |
|
bb5d6269 |
nameObj, ok := session.Values()[UserNameKey]
if !ok {
return nil, false, nil
}
name, ok := nameObj.(string)
if !ok {
return nil, false, errors.New("user.name on session is not a string")
}
if name == "" {
return nil, false, nil
}
|
66c42785 |
uidObj, ok := session.Values()[UserUIDKey]
if !ok {
return nil, false, nil
}
uid, ok := uidObj.(string)
if !ok {
return nil, false, errors.New("user.uid on session is not a string")
}
// Tolerate empty string UIDs in the session
|
e2ffc795 |
return &user.DefaultInfo{ |
bb5d6269 |
Name: name, |
66c42785 |
UID: uid, |
bb5d6269 |
}, true, nil
} |
5fa43563 |
|
e2ffc795 |
func (a *Authenticator) AuthenticationSucceeded(user user.Info, state string, w http.ResponseWriter, req *http.Request) (bool, error) { |
5fa43563 |
session, err := a.store.Get(req, a.name)
if err != nil { |
e1eecdfc |
return false, err |
5fa43563 |
}
values := session.Values()
values[UserNameKey] = user.GetName() |
66c42785 |
values[UserUIDKey] = user.GetUID()
// TODO: should we save groups, scope, and extra in the session as well? |
e1eecdfc |
return false, a.store.Save(w, req) |
5fa43563 |
} |
441b50a5 |
|
3aac8953 |
func (a *Authenticator) InvalidateAuthentication(w http.ResponseWriter, req *http.Request) error { |
441b50a5 |
session, err := a.store.Get(req, a.name)
if err != nil {
return err
}
session.Values()[UserNameKey] = "" |
66c42785 |
session.Values()[UserUIDKey] = "" |
441b50a5 |
return a.store.Save(w, req)
} |