#lang racket
(require net/base64
web-server/stuffers/hmac-sha1
"util.rkt")
(define public-key (make-parameter ""))
(define private-key (make-parameter ""))
(provide public-key
private-key)
(define/provide (read-keys [file
(build-path (find-system-path 'home-dir)
".aws-keys")])
(match (file->lines file #:mode 'text #:line-mode 'any)
[(list (regexp #rx"^(?i:AWSAccessKeyId)=(.*)$" (list _ public))
(regexp #rx"^(?i:AWSSecretKey)=(.*)$" (list _ private))
_ ...)
(public-key public)
(private-key private)]
[(list public
private
_ ...)
(public-key public)
(private-key private)]
[else (error 'read-keys
(string-append
"First two lines of file must be:\n"
"AWSAccessKeyId=<key>\n"
"AWSSecretKey=<key>\n"))]))
(define/provide (ensure-have-keys)
(define (keys-blank?)
(or (string=? "" (public-key))
(string=? "" (private-key))))
(when (keys-blank?)
(read-keys))
(when (keys-blank?)
(error 'ensure-have-keys
(string-append "Set the parameters `public-key' and "
"`private-key' to the AWS AccessKeyID "
"and SecretKey, respectively. "
"Tip: `(read-keys)' will read them "
"from a ~~/.aws-keys file."))))
(define/contract/provide (sha1-encode str)
(string? . -> . string?)
(match (bytes->string/utf-8
(base64-encode (HMAC-SHA1 (string->bytes/utf-8 (private-key))
(string->bytes/utf-8 str))))
[(regexp #rx"^(.*)\r\n$" (list _ x)) x] [s s]))