func GenerateSignature(c *cli.Context) { url := c.String("url") if url == "" { cli.ShowCommandHelp(c, "generate") os.Exit(1) } crypto, err := common.CreateCrypto(c) if err != nil { os.Exit(1) } signature, err := createSigFromArgs(c) if err != nil { os.Exit(1) } sigEncoded, metaEncoded, err := header.BuildSignatureAndMetadata(crypto, &signature) if err != nil { fmt.Printf("Failed to create signature: %s", err.Error()) os.Exit(1) } fmt.Printf("Encoded Signature:\n%s\n\n", sigEncoded) fmt.Printf("Encoded Metadata:\n%s\n\n", metaEncoded) }
func (rs *RouteServiceConfig) GenerateSignatureAndMetadata(forwardedUrlRaw string) (string, string, error) { signature := &header.Signature{ RequestedTime: time.Now(), ForwardedUrl: forwardedUrlRaw, } signatureHeader, metadataHeader, err := header.BuildSignatureAndMetadata(rs.crypto, signature) if err != nil { return "", "", err } return signatureHeader, metadataHeader, nil }
return []byte(decryptedStr), nil } crypto.EncryptStub = func(plainText []byte) ([]byte, []byte, error) { nonce := []byte("some-nonce") cipherText := append(plainText, "encrypted"...) cipherText = append(cipherText, nonce...) return cipherText, nonce, nil } signature = &header.Signature{RequestedTime: time.Now()} }) Describe("Build Signature and Metadata", func() { It("builds signature and metadata headers", func() { signatureHeader, metadata, err := header.BuildSignatureAndMetadata(crypto, signature) Expect(err).ToNot(HaveOccurred()) Expect(signatureHeader).ToNot(BeNil()) metadataDecoded, err := base64.URLEncoding.DecodeString(metadata) Expect(err).ToNot(HaveOccurred()) metadataStruct := header.Metadata{} err = json.Unmarshal([]byte(metadataDecoded), &metadataStruct) Expect(err).ToNot(HaveOccurred()) Expect(metadataStruct.Nonce).To(Equal([]byte("some-nonce"))) }) Context("when unable to encrypt the signature", func() { BeforeEach(func() { crypto.EncryptReturns([]byte{}, []byte{}, errors.New("No entropy")) })