Ejemplo n.º 1
0
func aboutPage(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	fmt.Fprintf(w, "<h1>%v</h1>", appengine.DefaultVersionHostname(c))

	token, expire, _ := appengine.AccessToken(c, "test")
	fmt.Fprintf(w, "<p>AccessToken: %v %v", token, expire)

	fmt.Fprintf(w, "<p>AppID: %v", appengine.AppID(c))
	fmt.Fprintf(w, "<p>FQAppID: %v", c.FullyQualifiedAppID())
	fmt.Fprintf(w, "<p>Go version: %v", runtime.Version())
	fmt.Fprintf(w, "<p>Datacenter: %v", appengine.Datacenter())
	fmt.Fprintf(w, "<p>InstanceID: %v", appengine.InstanceID())
	fmt.Fprintf(w, "<p>IsDevAppServer: %v", appengine.IsDevAppServer())
	fmt.Fprintf(w, "<p>RequestID: %v", appengine.RequestID(c))
	fmt.Fprintf(w, "<p>ServerSoftware: %v", appengine.ServerSoftware())

	sa, _ := appengine.ServiceAccount(c)
	fmt.Fprintf(w, "<p>ServiceAccount: %v", sa)

	keyname, signed, _ := appengine.SignBytes(c, []byte("test"))
	fmt.Fprintf(w, "<p>SignBytes: %v %v", keyname, signed)
	fmt.Fprintf(w, "<p>VersionID: %v", appengine.VersionID(c))

	fmt.Fprintf(w, "<p>Request: %v", r)
	r2 := c.Request()
	fmt.Fprintf(w, "<p>Context Request type/value: %T %v", r2, r2)
}
Ejemplo n.º 2
0
// FetchToken fetches a new access token for the provided scopes.
func (c *AppEngineConfig) FetchToken(existing *oauth2.Token) (*oauth2.Token, error) {
	token, expiry, err := appengine.AccessToken(c.context, strings.Join(c.scopes, " "))
	if err != nil {
		return nil, err
	}
	return &oauth2.Token{
		AccessToken: token,
		Expiry:      expiry,
	}, nil
}
Ejemplo n.º 3
0
// accessToken returns an access token for the given scope and caches it.
func accessToken(c appengine.Context, scope string) (string, error) {
	if token, err := memcache.Get(c, "token"); err == nil {
		return string(token.Value), nil
	}
	tok, expiry, err := appengine.AccessToken(c, scope)
	if err != nil {
		return "", err
	}
	// Ignore memcache errors and return the access token.
	memcache.Set(c, &memcache.Item{
		Key:        "token",
		Value:      []byte(tok),
		Expiration: expiry.Sub(time.Now()),
	})
	return tok, nil
}
Ejemplo n.º 4
0
func (t *transport) Refresh() error {
	// Get a new access token for the application service account.
	tok, expiry, err := appengine.AccessToken(t.Context, t.Scopes...)
	if err != nil {
		return err
	}
	t.Token = &oauth.Token{
		AccessToken: tok,
		Expiry:      expiry,
	}
	if t.TokenCache != nil {
		// Cache the token and ignore error (as we can always get a new one).
		t.TokenCache.PutToken(t.Token)
	}
	return nil
}
Ejemplo n.º 5
0
func getGcsContext(c appengine.Context) (ctx context.Context, err error) {

	// This turns out to be the way to get the GCS client library to work
	accessToken, _, err := appengine.AccessToken(c, storage.ScopeFullControl)
	if err != nil {
		return nil, err
	}

	hc := &http.Client{}
	hc.Transport = &oauth2.Transport{
		Base: &urlfetch.Transport{
			Context: c,
		},
		Source: oauth2.StaticTokenSource(&oauth2.Token{
			AccessToken: accessToken,
		}),
	}

	ctx = cloud.NewContext(appengine.AppID(c), hc)
	return ctx, nil
}
Ejemplo n.º 6
0
import (
	"time"

	"appengine"

	"golang.org/x/oauth2"
)

// AppEngineTokenSource returns a token source that fetches tokens
// issued to the current App Engine application's service account.
// If you are implementing a 3-legged OAuth 2.0 flow on App Engine
// that involves user accounts, see oauth2.Config instead.
//
// You are required to provide a valid appengine.Context as context.
func AppEngineTokenSource(ctx appengine.Context, scope ...string) oauth2.TokenSource {
	return &appEngineTokenSource{
		ctx:         ctx,
		scopes:      scope,
		fetcherFunc: aeFetcherFunc,
	}
}

var aeFetcherFunc = func(ctx oauth2.Context, scope ...string) (string, time.Time, error) {
	c, ok := ctx.(appengine.Context)
	if !ok {
		return "", time.Time{}, errInvalidContext
	}
	return appengine.AccessToken(c, scope...)
}