1// Copyright 2014 Google Inc. All rights reserved.
2// Use of this source code is governed by the Apache 2.0
3// license that can be found in the LICENSE file.
4
5// +build !appengine
6
7package user
8
9import (
10	"golang.org/x/net/context"
11
12	"google.golang.org/appengine/internal"
13)
14
15// Current returns the currently logged-in user,
16// or nil if the user is not signed in.
17func Current(c context.Context) *User {
18	h := internal.IncomingHeaders(c)
19	u := &User{
20		Email:             h.Get("X-AppEngine-User-Email"),
21		AuthDomain:        h.Get("X-AppEngine-Auth-Domain"),
22		ID:                h.Get("X-AppEngine-User-Id"),
23		Admin:             h.Get("X-AppEngine-User-Is-Admin") == "1",
24		FederatedIdentity: h.Get("X-AppEngine-Federated-Identity"),
25		FederatedProvider: h.Get("X-AppEngine-Federated-Provider"),
26	}
27	if u.Email == "" && u.FederatedIdentity == "" {
28		return nil
29	}
30	return u
31}
32
33// IsAdmin returns true if the current user is signed in and
34// is currently registered as an administrator of the application.
35func IsAdmin(c context.Context) bool {
36	h := internal.IncomingHeaders(c)
37	return h.Get("X-AppEngine-User-Is-Admin") == "1"
38}
39