sigs.k8s.io/controller-runtime@v0.18.2/pkg/webhook/authentication/response.go (about) 1 /* 2 Copyright 2021 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package authentication 18 19 import ( 20 authenticationv1 "k8s.io/api/authentication/v1" 21 ) 22 23 // Authenticated constructs a response indicating that the given token 24 // is valid. 25 func Authenticated(reason string, user authenticationv1.UserInfo) Response { 26 return ReviewResponse(true, user, reason) 27 } 28 29 // Unauthenticated constructs a response indicating that the given token 30 // is not valid. 31 func Unauthenticated(reason string, user authenticationv1.UserInfo) Response { 32 return ReviewResponse(false, authenticationv1.UserInfo{}, reason) 33 } 34 35 // Errored creates a new Response for error-handling a request. 36 func Errored(err error) Response { 37 return Response{ 38 TokenReview: authenticationv1.TokenReview{ 39 Spec: authenticationv1.TokenReviewSpec{}, 40 Status: authenticationv1.TokenReviewStatus{ 41 Authenticated: false, 42 Error: err.Error(), 43 }, 44 }, 45 } 46 } 47 48 // ReviewResponse returns a response for admitting a request. 49 func ReviewResponse(authenticated bool, user authenticationv1.UserInfo, err string, audiences ...string) Response { 50 resp := Response{ 51 TokenReview: authenticationv1.TokenReview{ 52 Status: authenticationv1.TokenReviewStatus{ 53 Authenticated: authenticated, 54 User: user, 55 Audiences: audiences, 56 }, 57 }, 58 } 59 if len(err) > 0 { 60 resp.TokenReview.Status.Error = err 61 } 62 return resp 63 }