github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/repository/authorization.go (about)

     1  package repository
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  
     7  	"github.com/golang/protobuf/proto"
     8  
     9  	"github.com/hoffie/larasync/repository/odf"
    10  )
    11  
    12  // Authorization is being used to pass the required data
    13  // to authorize a new client to the server system.
    14  type Authorization struct {
    15  	SigningKey    [PrivateKeySize]byte
    16  	EncryptionKey [EncryptionKeySize]byte
    17  	HashingKey    [HashingKeySize]byte
    18  }
    19  
    20  // newAuthorizationFromPb returns a new Authorization object
    21  // from the protobuf definition.
    22  func newAuthorizationFromPb(pbAuthorization *odf.Authorization) *Authorization {
    23  	auth := &Authorization{
    24  		SigningKey:    [PrivateKeySize]byte{},
    25  		EncryptionKey: [EncryptionKeySize]byte{},
    26  		HashingKey:    [HashingKeySize]byte{},
    27  	}
    28  
    29  	auth.setFromPb(pbAuthorization)
    30  
    31  	return auth
    32  }
    33  
    34  // setFromPb is used to copy data from a protobuf Authorization to the
    35  // this Authorization struct.
    36  func (a *Authorization) setFromPb(pbAuthorization *odf.Authorization) {
    37  	protoSigningKey := pbAuthorization.GetSigningKey()
    38  	protoEncryptionKey := pbAuthorization.GetEncryptionKey()
    39  	protoHashingKey := pbAuthorization.GetHashingKey()
    40  
    41  	copy(a.SigningKey[:], protoSigningKey[0:PrivateKeySize])
    42  	copy(a.EncryptionKey[:], protoEncryptionKey[0:EncryptionKeySize])
    43  	copy(a.HashingKey[:], protoHashingKey[0:HashingKeySize])
    44  }
    45  
    46  // toPb converts this Authorization to a protobuf Authorization.
    47  // This is used by the encoder.
    48  func (a *Authorization) toPb() (*odf.Authorization, error) {
    49  	signingKey := make([]byte, PrivateKeySize)
    50  	encryptionKey := make([]byte, EncryptionKeySize)
    51  	hashingKey := make([]byte, HashingKeySize)
    52  
    53  	copy(signingKey[:], a.SigningKey[:PrivateKeySize])
    54  	copy(encryptionKey[:], a.EncryptionKey[:EncryptionKeySize])
    55  	copy(hashingKey[:], a.HashingKey[:HashingKeySize])
    56  
    57  	protoAuthorization := &odf.Authorization{
    58  		SigningKey:    signingKey,
    59  		EncryptionKey: encryptionKey,
    60  		HashingKey:    hashingKey,
    61  	}
    62  
    63  	return protoAuthorization, nil
    64  }
    65  
    66  // ReadFrom fills this Authorization's data with the contents supplied by
    67  // the binary representation available through the given reader.
    68  func (a *Authorization) ReadFrom(r io.Reader) (int64, error) {
    69  	buf := &bytes.Buffer{}
    70  	read, err := io.Copy(buf, r)
    71  	if err != nil {
    72  		return read, err
    73  	}
    74  	pb := &odf.Authorization{}
    75  	err = proto.Unmarshal(buf.Bytes(), pb)
    76  	if err != nil {
    77  		return read, err
    78  	}
    79  
    80  	a.setFromPb(pb)
    81  	return read, nil
    82  }
    83  
    84  // WriteTo encodes this Authorization to the supplied Writer in binary form.
    85  // Returns the number of bytes written and an error if applicable.
    86  func (a *Authorization) WriteTo(w io.Writer) (int64, error) {
    87  	pb, err := a.toPb()
    88  	if err != nil {
    89  		return 0, err
    90  	}
    91  
    92  	buf, err := proto.Marshal(pb)
    93  
    94  	if err != nil {
    95  		return 0, err
    96  	}
    97  	written, err := io.Copy(w, bytes.NewBuffer(buf))
    98  	return written, err
    99  }