-
-
Notifications
You must be signed in to change notification settings - Fork 10
RFC: add signature v4 #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f800c60
f3c9732
06b4760
974c623
4c34768
f070eaa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
## Installation | ||
|
||
```bash | ||
npm install --save @effect-aws/signature-v4 | ||
``` | ||
|
||
_Note_: depending on the chosen implementation either `aws4fetch` or `@smithy/signature-v4` dependency is required | ||
|
||
## Usage | ||
|
||
1. Let's start with creating AWS credentials layer. | ||
Credentials have type `Ref<Option<AWSCredentials>>`, making it possible to update it's value at any moment during app lifetime | ||
|
||
```typescript | ||
import { Credentials } from '@effect-aws/signature-v4/Credentials' | ||
import awsCredentials from './credentials.ts' | ||
|
||
|
||
const CredentialsLayer = Credentials.layer(awsCredentials) | ||
|
||
/** | ||
* We can access or modify credentials directly from within our Effect app like this | ||
* | ||
*/ | ||
Effect.gen(function* () { | ||
/** | ||
* Get credentials | ||
*/ | ||
const credentials: Option<AWSCredentials> = yield* Credentials.pipe( | ||
Effect.andThen(Ref.get) | ||
) | ||
/** | ||
* Populate credentials with new value | ||
*/ | ||
yield* Credentials.pipe( | ||
Effect.andThen(Ref.set(Option.some(newCredentials))) | ||
) | ||
|
||
// or via handy API | ||
const credentials = yield* Credentials.current | ||
yield* Credentials.update(newCredentials) | ||
}).pipe( | ||
Effect.provide(CredentialsLayer) | ||
) | ||
``` | ||
|
||
_Note_: secretAccessKey and sessionToken credentials values are wrapped with `Redacted` | ||
|
||
2. Use signer of choice to sign HttpClientRequest | ||
```typescript | ||
import { SignatureV4 } from '@effect-aws/signature-v4/Smithy' | ||
// or | ||
import { SignatureV4 } from '@effect-aws/signature-v4/Aws4Fetch' | ||
``` | ||
|
||
_Note_: avoid importing an implementation via barrel-import due to different set of peer dependencies | ||
|
||
```typescript | ||
const SigV4 = SignatureV4.Default.pipe( | ||
Effect.provideMerge(CredentialsLayer) // or Effect.provide if you're not planning to access credentials directly in your code | ||
) | ||
|
||
Effect.gen(function* () { | ||
// you got a hold of an HttpClientRequest | ||
const signedRequest = yield* SignatureV4.signRequest(request) | ||
|
||
// or use transformClient to modify and HttpClient to automatically sign requests | ||
|
||
const { transformClient } = yield* SignatureV4 | ||
|
||
const apiClient = yield* HttpApiClient.make(Api, { | ||
baseUrl, | ||
transformClient, | ||
}) | ||
}).pipe( | ||
Layer.provide(SigV4) | ||
) | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you clarify why do you need
Ref
I feel like using Ref with Layer is a bit off, it makes it impure.
I would rather use the
globalValue
and 2 functions:withCredentials
andsetCredentials
(hof and setter). see example here, however global value stripsCredentials
as dependency and requires default value, which suits well if you use shell aws config or sso...