| Index: milo/appengine/common/middleware.go
|
| diff --git a/milo/appengine/common/middleware.go b/milo/appengine/common/middleware.go
|
| index daef78d87690eb9cab334f9cff9f673c2a3beeb2..c5fb5e853e77f399db2a90982e0d29e5e5ba34c0 100644
|
| --- a/milo/appengine/common/middleware.go
|
| +++ b/milo/appengine/common/middleware.go
|
| @@ -8,14 +8,25 @@ import (
|
| "net/http"
|
| "strings"
|
|
|
| + "cloud.google.com/go/compute/metadata"
|
| + "cloud.google.com/go/datastore"
|
| +
|
| "golang.org/x/net/context"
|
| + "golang.org/x/oauth2"
|
| + "golang.org/x/oauth2/google"
|
|
|
| + "github.com/luci/gae/impl/cloud"
|
| + "github.com/luci/gae/impl/memory"
|
| "github.com/luci/gae/service/info"
|
| "github.com/luci/luci-go/appengine/gaeauth/server"
|
| + "github.com/luci/luci-go/appengine/gaeauth/server/gaesigner"
|
| "github.com/luci/luci-go/appengine/gaemiddleware"
|
| + "github.com/luci/luci-go/appengine/gaesecrets"
|
| "github.com/luci/luci-go/common/clock"
|
| + "github.com/luci/luci-go/common/logging/gologger"
|
| "github.com/luci/luci-go/server/analytics"
|
| "github.com/luci/luci-go/server/auth"
|
| + "github.com/luci/luci-go/server/auth/authdb"
|
| "github.com/luci/luci-go/server/auth/identity"
|
| "github.com/luci/luci-go/server/router"
|
| "github.com/luci/luci-go/server/templates"
|
| @@ -54,14 +65,76 @@ func GetTemplateBundle() *templates.Bundle {
|
| }
|
| }
|
|
|
| -// Base returns the basic LUCI appengine middlewares.
|
| -func Base() router.MiddlewareChain {
|
| - methods := auth.Authenticator{
|
| +// authMethods returns the set of authentication methods used for Milo.
|
| +func authMethods() auth.Authenticator {
|
| + return auth.Authenticator{
|
| &server.OAuth2Method{Scopes: []string{server.EmailScope}},
|
| server.CookieAuth,
|
| &server.InboundAppIDAuthMethod{},
|
| }
|
| - m := gaemiddleware.BaseProd().Extend(auth.Use(methods), auth.Authenticate)
|
| +}
|
| +
|
| +// Flex returns the basic middleware for use on appengine flex. Flex does not
|
| +// allow the use of appengine APIs.
|
| +func FlexBase() router.MiddlewareChain {
|
| + // Get the name of this project from the metadata server, since we're on GCE.
|
| + project, err := metadata.Get("project/project-id")
|
| + if err != nil {
|
| + panic(err)
|
| + }
|
| + // Use the standard Go logger.
|
| + // TODO(hinoka): Use the cloud logger, somehow.
|
| + logger := func(c *router.Context, next router.Handler) {
|
| + c.Context = gologger.StdConfig.Use(c.Context)
|
| + next(c)
|
| + }
|
| + // The base set of services uses memory implementations.
|
| + memory := func(c *router.Context, next router.Handler) {
|
| + c.Context = memory.UseWithAppID(c.Context, project)
|
| + next(c)
|
| + }
|
| + // Use the cloud datastore client.
|
| + ds := func(c *router.Context, next router.Handler) {
|
| + client, err := datastore.NewClient(c.Context, project)
|
| + if err != nil {
|
| + panic(err)
|
| + }
|
| + c.Context = cloud.UseDatastore(c.Context, client)
|
| + next(c)
|
| + }
|
| + // Secret store, used for decryping OID tokens, which is needed for cookieauth.
|
| + secrets := func(c *router.Context, next router.Handler) {
|
| + c.Context = gaesecrets.Use(c.Context, nil)
|
| + next(c)
|
| + }
|
| + // Swap out the default auth configs with one that uses a in-memory cache, since
|
| + // we can't use memcache.
|
| + authConfig := func(c *router.Context, next router.Handler) {
|
| + mustToken := func(c context.Context, scopes []string) (*oauth2.Token, error) {
|
| + // Ignore context and scope since its not needed.
|
| + return google.ComputeTokenSource("").Token()
|
| + }
|
| + config := auth.Config{
|
| + DBProvider: authdb.NewDBCache(server.GetAuthDB),
|
| + Signer: gaesigner.Signer{},
|
| + AccessTokenProvider: mustToken,
|
| + AnonymousTransport: func(c context.Context) http.RoundTripper {
|
| + return http.DefaultTransport
|
| + },
|
| + Cache: auth.MemoryCache(10),
|
| + IsDevMode: false,
|
| + }
|
| + c.Context = auth.SetConfig(c.Context, config)
|
| + next(c)
|
| + }
|
| + // Now chain it all together!
|
| + return router.NewMiddlewareChain(
|
| + logger, memory, ds, secrets, auth.Use(authMethods()), authConfig, auth.Authenticate)
|
| +}
|
| +
|
| +// Base returns the basic LUCI appengine middlewares.
|
| +func Base() router.MiddlewareChain {
|
| + m := gaemiddleware.BaseProd().Extend(auth.Use(authMethods()), auth.Authenticate)
|
| m = m.Extend(withRequestMiddleware)
|
| m = m.Extend(templates.WithTemplates(GetTemplateBundle()))
|
| return m
|
|
|