...

Source file src/k8s.io/cluster-bootstrap/token/jws/jws.go

Documentation: k8s.io/cluster-bootstrap/token/jws

     1  /*
     2  Copyright 2016 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 bootstrap
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  
    23  	jose "gopkg.in/square/go-jose.v2"
    24  )
    25  
    26  // ComputeDetachedSignature takes content and token details and computes a detached
    27  // JWS signature.  This is described in Appendix F of RFC 7515.  Basically, this
    28  // is a regular JWS with the content part of the signature elided.
    29  func ComputeDetachedSignature(content, tokenID, tokenSecret string) (string, error) {
    30  	jwk := &jose.JSONWebKey{
    31  		Key:   []byte(tokenSecret),
    32  		KeyID: tokenID,
    33  	}
    34  
    35  	opts := &jose.SignerOptions{
    36  		// Since this is a symmetric key, go-jose doesn't automatically include
    37  		// the KeyID as part of the protected header. We have to pass it here
    38  		// explicitly.
    39  		ExtraHeaders: map[jose.HeaderKey]interface{}{
    40  			"kid": tokenID,
    41  		},
    42  	}
    43  
    44  	signer, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.HS256, Key: jwk}, opts)
    45  	if err != nil {
    46  		return "", fmt.Errorf("can't make a HS256 signer from the given token: %v", err)
    47  	}
    48  
    49  	jws, err := signer.Sign([]byte(content))
    50  	if err != nil {
    51  		return "", fmt.Errorf("can't HS256-sign the given token: %v", err)
    52  	}
    53  
    54  	fullSig, err := jws.CompactSerialize()
    55  	if err != nil {
    56  		return "", fmt.Errorf("can't serialize the given token: %v", err)
    57  	}
    58  	return stripContent(fullSig)
    59  }
    60  
    61  // stripContent will remove the content part of a compact JWS
    62  //
    63  // The `go-jose` library doesn't support generating signatures with "detached"
    64  // content. To make up for this we take the full compact signature, break it
    65  // apart and put it back together without the content section.
    66  func stripContent(fullSig string) (string, error) {
    67  	parts := strings.Split(fullSig, ".")
    68  	if len(parts) != 3 {
    69  		return "", fmt.Errorf("compact JWS format must have three parts")
    70  	}
    71  
    72  	return parts[0] + ".." + parts[2], nil
    73  }
    74  
    75  // DetachedTokenIsValid checks whether a given detached JWS-encoded token matches JWS output of the given content and token
    76  func DetachedTokenIsValid(detachedToken, content, tokenID, tokenSecret string) bool {
    77  	newToken, err := ComputeDetachedSignature(content, tokenID, tokenSecret)
    78  	if err != nil {
    79  		return false
    80  	}
    81  	return detachedToken == newToken
    82  }
    83  

View as plain text