github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/creds/jwk.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     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 creds
    16  
    17  import (
    18  	"encoding/base64"
    19  	"encoding/json"
    20  	"io"
    21  	"io/ioutil"
    22  	"path/filepath"
    23  
    24  	"github.com/dolthub/dolt/go/libraries/utils/filesys"
    25  	"github.com/dolthub/dolt/go/libraries/utils/iohelp"
    26  )
    27  
    28  const (
    29  	JWKFileExtension = ".jwk"
    30  	ed25519Crv       = "Ed25519"
    31  	kty              = "OKP"
    32  )
    33  
    34  type jwkData struct {
    35  	D   *string `json:"d"`
    36  	X   *string `json:"x"`
    37  	Kty string  `json:"kty"`
    38  	Crv string  `json:"crv"`
    39  }
    40  
    41  func JWKCredSerialize(dc DoltCreds) ([]byte, error) {
    42  	if !dc.IsPubKeyValid() {
    43  		panic("public key missing or invalid.  This is a bug.  Should be validated before calling")
    44  	}
    45  
    46  	pubKeyStr := base64.URLEncoding.EncodeToString(dc.PubKey)
    47  
    48  	var privKeyStr string
    49  	if dc.HasPrivKey() {
    50  		if !dc.IsPrivKeyValid() {
    51  			panic("Invalid private key. This is a bug. Should be validated before calling")
    52  		}
    53  
    54  		privKeyStr = base64.URLEncoding.EncodeToString(dc.PrivKey)
    55  	}
    56  
    57  	toSerialize := jwkData{&pubKeyStr, &privKeyStr, kty, ed25519Crv}
    58  	data, err := json.Marshal(toSerialize)
    59  
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	return data, nil
    65  }
    66  
    67  func JWKCredsDeserialize(data []byte) (DoltCreds, error) {
    68  	var jwk jwkData
    69  	err := json.Unmarshal(data, &jwk)
    70  
    71  	if err == nil {
    72  		var pub, priv []byte
    73  		pub, err = base64.URLEncoding.DecodeString(*jwk.D)
    74  
    75  		if err == nil {
    76  			if jwk.X != nil {
    77  				priv, err = base64.URLEncoding.DecodeString(*jwk.X)
    78  			}
    79  
    80  			if err == nil {
    81  				kid := PubKeyToKID(pub)
    82  				return DoltCreds{pub, priv, kid}, nil
    83  			}
    84  		}
    85  	}
    86  
    87  	return DoltCreds{}, err
    88  }
    89  
    90  func JWKCredsWrite(wr io.Writer, dc DoltCreds) error {
    91  	data, err := JWKCredSerialize(dc)
    92  
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	return iohelp.WriteAll(wr, data)
    98  }
    99  
   100  func JWKCredsRead(rd io.Reader) (DoltCreds, error) {
   101  	data, err := ioutil.ReadAll(rd)
   102  
   103  	if err != nil {
   104  		return DoltCreds{}, err
   105  	}
   106  
   107  	return JWKCredsDeserialize(data)
   108  }
   109  
   110  func JWKCredsWriteToDir(fs filesys.Filesys, dir string, dc DoltCreds) (string, error) {
   111  	outFile := filepath.Join(dir, dc.KeyIDBase32Str()+JWKFileExtension)
   112  	wr, err := fs.OpenForWrite(outFile, 0600)
   113  
   114  	if err != nil {
   115  		return "", err
   116  	}
   117  
   118  	err = JWKCredsWrite(wr, dc)
   119  	if err == nil {
   120  		err = wr.Close()
   121  	} else {
   122  		wr.Close()
   123  	}
   124  
   125  	return outFile, err
   126  }
   127  
   128  func JWKCredsReadFromFile(fs filesys.Filesys, path string) (DoltCreds, error) {
   129  	rd, err := fs.OpenForRead(path)
   130  
   131  	if err != nil {
   132  		return DoltCreds{}, err
   133  	}
   134  
   135  	defer rd.Close()
   136  	return JWKCredsRead(rd)
   137  }