github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/repository/authorizationManager.go (about) 1 package repository 2 3 import ( 4 "bytes" 5 "encoding/hex" 6 "io" 7 "io/ioutil" 8 9 "github.com/hoffie/larasync/helpers/crypto" 10 "github.com/hoffie/larasync/repository/content" 11 ) 12 13 // AuthorizationManager handles the Authorizations of a specific 14 // 15 type AuthorizationManager struct { 16 storage content.Storage 17 } 18 19 func newAuthorizationManager(storage content.Storage) *AuthorizationManager { 20 return &AuthorizationManager{ 21 storage: storage, 22 } 23 } 24 25 // Set adds the authorization to the given backend and encrypts it 26 // first. 27 func (am *AuthorizationManager) Set( 28 signaturePubKey [PublicKeySize]byte, 29 encryptionKey [EncryptionKeySize]byte, 30 authorization *Authorization, 31 ) error { 32 enc, err := am.Serialize(encryptionKey, authorization) 33 if err != nil { 34 return err 35 } 36 37 return am.SetData(signaturePubKey, bytes.NewReader(enc)) 38 } 39 40 // Serialize encrypts the Authorization with the given secret 41 // and returns the encrypted result. 42 func (am *AuthorizationManager) Serialize( 43 encryptionKey [EncryptionKeySize]byte, 44 authorization *Authorization, 45 ) ([]byte, error) { 46 data := &bytes.Buffer{} 47 _, err := authorization.WriteTo(data) 48 if err != nil { 49 return nil, err 50 } 51 52 box := crypto.NewBox(encryptionKey) 53 enc, err := box.EncryptWithRandomKey(data.Bytes()) 54 if err != nil { 55 return nil, err 56 } 57 return enc, nil 58 } 59 60 // SetData adds for the already encrypted byte data and the given public key 61 // to the storage backend. 62 func (am *AuthorizationManager) SetData( 63 pubKey [PublicKeySize]byte, 64 reader io.Reader, 65 ) error { 66 pubKeyString := hex.EncodeToString(pubKey[:]) 67 return am.storage.Set(pubKeyString, reader) 68 } 69 70 // GetReaderString returns the reader for the given publicKey string representation. 71 func (am *AuthorizationManager) GetReaderString(key string) (io.ReadCloser, error) { 72 byteKey, err := hex.DecodeString(key) 73 if err != nil { 74 return nil, err 75 } 76 77 if len(byteKey) != PublicKeySize { 78 return nil, ErrInvalidPublicKeySize 79 } 80 inputKey := [PublicKeySize]byte{} 81 copy(inputKey[:], byteKey) 82 83 return am.GetReader(inputKey) 84 } 85 86 // GetReader returns a reader for the authorization stored with the passed PublicKey. 87 func (am *AuthorizationManager) GetReader(key [PublicKeySize]byte) (io.ReadCloser, error) { 88 publicKeyString := hex.EncodeToString(key[:]) 89 return am.storage.Get(publicKeyString) 90 } 91 92 // Get returns the Authorization for the given public Signature Key and 93 // tries to decrypt it with the passed encryptionKey. 94 func (am *AuthorizationManager) Get( 95 signaturePubKey [PublicKeySize]byte, 96 encryptionKey [EncryptionKeySize]byte, 97 ) (*Authorization, error) { 98 reader, err := am.GetReader(signaturePubKey) 99 if err != nil { 100 return nil, err 101 } 102 defer reader.Close() 103 104 enc, err := ioutil.ReadAll(reader) 105 if err != nil { 106 return nil, err 107 } 108 109 box := crypto.NewBox(encryptionKey) 110 data, err := box.DecryptContent(enc) 111 if err != nil { 112 return nil, err 113 } 114 115 auth := &Authorization{} 116 _, err = auth.ReadFrom(bytes.NewBuffer(data)) 117 if err != nil { 118 return nil, err 119 } 120 121 _ = reader.Close() 122 123 return auth, nil 124 } 125 126 // ExistsForString returns if there is a authorization stored 127 // for a given publicKey string representation. 128 func (am *AuthorizationManager) ExistsForString(publicKey string) bool { 129 return am.storage.Exists(publicKey) 130 } 131 132 // Exists returns if there is a key existing for the given 133 // publicKey. 134 func (am *AuthorizationManager) Exists(key [PublicKeySize]byte) bool { 135 keyString := hex.EncodeToString(key[:]) 136 return am.ExistsForString(keyString) 137 } 138 139 // DeleteForString deletes the authorization which is stored for the 140 // given publicKey string representation. 141 func (am *AuthorizationManager) DeleteForString(publicKey string) error { 142 return am.storage.Delete(publicKey) 143 } 144 145 // Delete removes the authorization which is stored for the signature 146 // which has the given PublicKey. 147 func (am *AuthorizationManager) Delete(key [PublicKeySize]byte) error { 148 keyString := hex.EncodeToString(key[:]) 149 return am.DeleteForString(keyString) 150 }