...

Source file src/github.com/sassoftware/relic/server/context.go

Documentation: github.com/sassoftware/relic/server

     1  //
     2  // Copyright (c) SAS Institute Inc.
     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 server
    18  
    19  import (
    20  	"net/http"
    21  	"strings"
    22  )
    23  
    24  type ctxKey int
    25  
    26  const (
    27  	ctxClientName ctxKey = iota
    28  	ctxRoles
    29  	ctxClientDN
    30  )
    31  
    32  func GetClientRoles(request *http.Request) []string {
    33  	roles := request.Context().Value(ctxRoles)
    34  	if roles == nil {
    35  		return nil
    36  	}
    37  	return roles.([]string)
    38  }
    39  
    40  func GetClientName(request *http.Request) string {
    41  	name := request.Context().Value(ctxClientName)
    42  	if name == nil {
    43  		return ""
    44  	}
    45  	return name.(string)
    46  }
    47  
    48  func GetClientDN(request *http.Request) string {
    49  	name := request.Context().Value(ctxClientDN)
    50  	if name == nil {
    51  		return ""
    52  	}
    53  	return name.(string)
    54  }
    55  
    56  func GetClientIP(request *http.Request) string {
    57  	address := request.RemoteAddr
    58  	colon := strings.LastIndex(address, ":")
    59  	if colon < 0 {
    60  		return address
    61  	}
    62  	address = address[:colon]
    63  	if address[0] == '[' && address[len(address)-1] == ']' {
    64  		address = address[1 : len(address)-1]
    65  	}
    66  	return address
    67  }
    68  

View as plain text