validateloginstatus.go 2.3 KB
Newer Older
1
// Copyright 2017 The Kubernetes Authors.
S
Sebastian Florek 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package validation

import (
	restful "github.com/emicklei/go-restful"
19
	"github.com/kubernetes/dashboard/src/app/backend/args"
S
Sebastian Florek 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32
	"github.com/kubernetes/dashboard/src/app/backend/client"
)

// LoginStatus is returned as a response to login status check. Used by the frontend to determine if is logged in
// and if login page should be shown.
type LoginStatus struct {
	// True when token header indicating logged in user is found in request.
	TokenPresent bool `json:"tokenPresent"`
	// True when authorization header indicating logged in user is found in request.
	HeaderPresent bool `json:"headerPresent"`
	// True if dashboard is configured to use HTTPS connection. It is required for secure
	// data exchange during login operation.
	HTTPSMode bool `json:"httpsMode"`
33 34 35 36 37
	// True if impersonation is enabled
	ImpersonationPresent bool `json:"impersonationPresent"`

	// The impersonated user
	ImpersonatedUser string `json:"impersonatedUser"`
S
Sebastian Florek 已提交
38 39 40
}

// ValidateLoginStatus returns information about user login status and if request was made over HTTPS.
41
func ValidateLoginStatus(request *restful.Request) *LoginStatus {
S
Sebastian Florek 已提交
42 43
	authHeader := request.HeaderParameter("Authorization")
	tokenHeader := request.HeaderParameter(client.JWETokenHeader)
44
	impersonationHeader := request.HeaderParameter("Impersonate-User")
S
Sebastian Florek 已提交
45

46
	httpsMode := request.Request.TLS != nil
47
	if args.Holder.GetEnableInsecureLogin() {
48 49 50
		httpsMode = true
	}

51 52 53 54 55 56 57 58 59
	loginStatus := &LoginStatus{
		TokenPresent:         len(tokenHeader) > 0,
		HeaderPresent:        len(authHeader) > 0,
		ImpersonationPresent: len(impersonationHeader) > 0,
		HTTPSMode:            httpsMode,
	}

	if loginStatus.ImpersonationPresent {
		loginStatus.ImpersonatedUser = impersonationHeader
S
Sebastian Florek 已提交
60
	}
61 62

	return loginStatus
S
Sebastian Florek 已提交
63
}