go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/auth/middleware.go (about) 1 // Copyright 2017 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package auth 16 17 import ( 18 "go.chromium.org/luci/server/router" 19 ) 20 21 // Authenticate returns a middleware that performs authentication. 22 // 23 // All given methods will be tried in order until the first applicable one. 24 // Typically there's only one method. 25 // 26 // This middleware either updates the context by injecting the authentication 27 // state into it (enabling functions like CurrentIdentity and IsMember), or 28 // aborts the request with an HTTP 401 or HTTP 500 error. 29 // 30 // Note that it passes through anonymous requests. CurrentIdentity returns 31 // identity.AnonymousIdentity in this case. Use separate authorization layer to 32 // further restrict the access, if necessary. 33 func Authenticate(m ...Method) router.Middleware { 34 if len(m) == 0 { 35 panic("at least one auth.Method is required") 36 } 37 for _, method := range m { 38 if method == nil { 39 panic("expecting auth.Method, got nil") 40 } 41 } 42 a := &Authenticator{Methods: m} 43 return a.GetMiddleware() 44 }