Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(359)

Side by Side Diff: milo/appengine/common/middleware.go

Issue 2830443003: auth: Refactor how authentication methods are passed to server/auth library. (Closed)
Patch Set: fix test Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package common 5 package common
6 6
7 import ( 7 import (
8 "net/http" 8 "net/http"
9 "strings" 9 "strings"
10 10
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 "Analytics": analytics.Snippet(c), 49 "Analytics": analytics.Snippet(c),
50 "RequestID": info.RequestID(c), 50 "RequestID": info.RequestID(c),
51 }, nil 51 }, nil
52 }, 52 },
53 FuncMap: funcMap, 53 FuncMap: funcMap,
54 } 54 }
55 } 55 }
56 56
57 // Base returns the basic LUCI appengine middlewares. 57 // Base returns the basic LUCI appengine middlewares.
58 func Base() router.MiddlewareChain { 58 func Base() router.MiddlewareChain {
59 » methods := auth.Authenticator{ 59 » return gaemiddleware.BaseProd().Extend(
60 » » &server.OAuth2Method{Scopes: []string{server.EmailScope}}, 60 » » auth.Authenticate(server.CookieAuth),
61 » » server.CookieAuth, 61 » » withRequestMiddleware,
62 » » &server.InboundAppIDAuthMethod{}, 62 » » templates.WithTemplates(GetTemplateBundle()),
63 » } 63 » )
64 » m := gaemiddleware.BaseProd().Extend(auth.Use(methods), auth.Authenticat e)
65 » m = m.Extend(withRequestMiddleware)
66 » m = m.Extend(templates.WithTemplates(GetTemplateBundle()))
67 » return m
68 } 64 }
69 65
70 // The context key, so that we can embed the http.Request object into 66 // The context key, so that we can embed the http.Request object into
71 // the context. 67 // the context.
72 var requestKey = "http.request" 68 var requestKey = "http.request"
73 69
74 // WithRequest returns a context with the http.Request object 70 // WithRequest returns a context with the http.Request object
75 // in it. 71 // in it.
76 func WithRequest(c context.Context, r *http.Request) context.Context { 72 func WithRequest(c context.Context, r *http.Request) context.Context {
77 return context.WithValue(c, &requestKey, r) 73 return context.WithValue(c, &requestKey, r)
78 } 74 }
79 75
80 // withRequestMiddleware is a middleware that installs a request into the contex t. 76 // withRequestMiddleware is a middleware that installs a request into the contex t.
81 // This is used for various things in the default template. 77 // This is used for various things in the default template.
82 func withRequestMiddleware(c *router.Context, next router.Handler) { 78 func withRequestMiddleware(c *router.Context, next router.Handler) {
83 c.Context = WithRequest(c.Context, c.Request) 79 c.Context = WithRequest(c.Context, c.Request)
84 next(c) 80 next(c)
85 } 81 }
86 82
87 func getRequest(c context.Context) *http.Request { 83 func getRequest(c context.Context) *http.Request {
88 if req, ok := c.Value(&requestKey).(*http.Request); ok { 84 if req, ok := c.Value(&requestKey).(*http.Request); ok {
89 return req 85 return req
90 } 86 }
91 panic("No http.request found in context") 87 panic("No http.request found in context")
92 } 88 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698