| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package ephelper | |
| 6 | |
| 7 import ( | |
| 8 "github.com/GoogleCloudPlatform/go-endpoints/endpoints" | |
| 9 gaeauth "github.com/luci/luci-go/appengine/gaeauth/server" | |
| 10 "github.com/luci/luci-go/appengine/gaemiddleware" | |
| 11 "github.com/luci/luci-go/server/auth" | |
| 12 "golang.org/x/net/context" | |
| 13 ) | |
| 14 | |
| 15 // Middleware is a Context manipulation function that is called when | |
| 16 // initializing a ServiceBase. | |
| 17 type Middleware func(context.Context) (context.Context, error) | |
| 18 | |
| 19 // TestMode is a no-op middleware layer. | |
| 20 var TestMode = []Middleware{} | |
| 21 | |
| 22 // DefaultMiddleware is the default middleware stack for a ServiceBase. It: | |
| 23 // | |
| 24 // - Installs the AppEngine production service base from | |
| 25 // gaemiddleware.WithProd. | |
| 26 // - Installs and authenticates the using the Authenticator methods from the | |
| 27 // ServiceBase. | |
| 28 func DefaultMiddleware(a auth.Authenticator) Middleware { | |
| 29 return func(c context.Context) (context.Context, error) { | |
| 30 c = gaemiddleware.WithProd(c, endpoints.HTTPRequest(c)) | |
| 31 | |
| 32 a := a | |
| 33 if a == nil { | |
| 34 mi := MethodInfo(c) | |
| 35 a = auth.Authenticator{ | |
| 36 &gaeauth.OAuth2Method{Scopes: mi.Scopes}, | |
| 37 } | |
| 38 } | |
| 39 c = auth.SetAuthenticator(c, a) | |
| 40 return a.Authenticate(c, endpoints.HTTPRequest(c)) | |
| 41 } | |
| 42 } | |
| OLD | NEW |