hyper-ts-oauth
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -5,2 +5,11 @@ # Changelog | ||
### [0.1.1](https://github.com/thewilkybarkid/hyper-ts-oauth/compare/v0.1.0...v0.1.1) (2022-05-24) | ||
### Features | ||
* add state to the authorization request ([f5e954e](https://github.com/thewilkybarkid/hyper-ts-oauth/commit/f5e954e76a87bd5ad6482bca7ae99bdf76b4e00b)) | ||
* decoder other details in the access token response ([3efd1ad](https://github.com/thewilkybarkid/hyper-ts-oauth/commit/3efd1adf5833fd3dc4723f981e70afc340138960)) | ||
* simplify authorization code type ([37ca0c6](https://github.com/thewilkybarkid/hyper-ts-oauth/commit/37ca0c6a840618e0f912605b617a1e30ab31d1d8)) | ||
## 0.1.0 (2022-05-23) | ||
@@ -7,0 +16,0 @@ |
@@ -5,6 +5,10 @@ /** | ||
import * as F from 'fetch-fp-ts'; | ||
import * as J from 'fp-ts/Json'; | ||
import * as RTE from 'fp-ts/ReaderTaskEither'; | ||
import { ResponseEnded, StatusOpen } from 'hyper-ts'; | ||
import * as RM from 'hyper-ts/lib/ReaderMiddleware'; | ||
import * as D from 'io-ts/Decoder'; | ||
import Decoder = D.Decoder; | ||
import FetchEnv = F.FetchEnv; | ||
import JsonRecord = J.JsonRecord; | ||
import ReaderMiddleware = RM.ReaderMiddleware; | ||
@@ -28,9 +32,2 @@ /** | ||
*/ | ||
export interface AuthorizationCode { | ||
readonly code: string; | ||
} | ||
/** | ||
* @category model | ||
* @since 0.1.0 | ||
*/ | ||
export interface AccessToken { | ||
@@ -46,3 +43,3 @@ readonly access_token: string; | ||
*/ | ||
export declare function requestAuthorizationCode(scope: string): ReaderMiddleware<OAuthEnv, StatusOpen, ResponseEnded, never, void>; | ||
export declare function requestAuthorizationCode(scope: string): (state?: string) => ReaderMiddleware<OAuthEnv, StatusOpen, ResponseEnded, never, void>; | ||
/** | ||
@@ -54,2 +51,3 @@ * Exchange an authorization code for an access token. | ||
*/ | ||
export declare function exchangeAuthorizationCode(code: AuthorizationCode): RTE.ReaderTaskEither<OAuthEnv & FetchEnv, unknown, AccessToken>; | ||
export declare function exchangeAuthorizationCode<A>(decoder: Decoder<JsonRecord, A>): (code: string) => RTE.ReaderTaskEither<OAuthEnv & FetchEnv, unknown, AccessToken & A>; | ||
export declare function exchangeAuthorizationCode(): (code: string) => RTE.ReaderTaskEither<OAuthEnv & FetchEnv, unknown, AccessToken>; |
@@ -49,13 +49,7 @@ "use strict"; | ||
function requestAuthorizationCode(scope) { | ||
return (0, function_1.pipe)(RM.rightReader(authorizationRequestUrl(scope)), RM.ichainW(RM.redirect), RM.ichain(() => RM.closeHeaders()), RM.ichain(() => RM.end())); | ||
return state => (0, function_1.pipe)(RM.rightReader(authorizationRequestUrl(scope, state)), RM.ichainW(RM.redirect), RM.ichain(() => RM.closeHeaders()), RM.ichain(() => RM.end())); | ||
} | ||
exports.requestAuthorizationCode = requestAuthorizationCode; | ||
/** | ||
* Exchange an authorization code for an access token. | ||
* | ||
* @category constructors | ||
* @since 0.1.0 | ||
*/ | ||
function exchangeAuthorizationCode(code) { | ||
return (0, function_1.pipe)(RTE.asks(({ oauth: { clientId, clientSecret, redirectUri, tokenUrl } }) => (0, function_1.pipe)(F.Request('POST')(tokenUrl), F.setBody(new URLSearchParams({ | ||
function exchangeAuthorizationCode(decoder = D.struct({})) { | ||
return (code) => (0, function_1.pipe)(RTE.asks(({ oauth: { clientId, clientSecret, redirectUri, tokenUrl } }) => (0, function_1.pipe)(F.Request('POST')(tokenUrl), F.setBody(new URLSearchParams({ | ||
client_id: clientId, | ||
@@ -65,4 +59,4 @@ client_secret: clientSecret, | ||
redirect_uri: redirectUri.href, | ||
code: code.code, | ||
}).toString(), hyper_ts_1.MediaType.applicationFormURLEncoded))), RTE.chainW(F.send), RTE.filterOrElseW(F.hasStatus(hyper_ts_1.Status.OK), function_1.identity), RTE.chainTaskEitherKW(F.decode((0, function_1.pipe)(JsonRecordD, D.compose(AccessTokenD))))); | ||
code, | ||
}).toString(), hyper_ts_1.MediaType.applicationFormURLEncoded))), RTE.chainW(F.send), RTE.filterOrElseW(F.hasStatus(hyper_ts_1.Status.OK), function_1.identity), RTE.chainTaskEitherKW(F.decode((0, function_1.pipe)(JsonRecordD, D.compose((0, function_1.pipe)(AccessTokenD, D.intersect(decoder))))))); | ||
} | ||
@@ -86,9 +80,15 @@ exports.exchangeAuthorizationCode = exchangeAuthorizationCode; | ||
// ------------------------------------------------------------------------------------- | ||
function authorizationRequestUrl(scope) { | ||
return R.asks(({ oauth: { authorizeUrl, clientId, redirectUri } }) => new URL(`?${new URLSearchParams({ | ||
client_id: clientId, | ||
response_type: 'code', | ||
redirect_uri: redirectUri.href, | ||
scope, | ||
}).toString()}`, authorizeUrl)); | ||
function authorizationRequestUrl(scope, state) { | ||
return R.asks(({ oauth: { authorizeUrl, clientId, redirectUri } }) => { | ||
const query = new URLSearchParams({ | ||
client_id: clientId, | ||
response_type: 'code', | ||
redirect_uri: redirectUri.href, | ||
scope, | ||
}); | ||
if (typeof state === 'string') { | ||
query.set('state', state); | ||
} | ||
return new URL(`?${query.toString()}`, authorizeUrl); | ||
}); | ||
} |
{ | ||
"name": "hyper-ts-oauth", | ||
"description": "hyper-ts middleware for OAuth 2.", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "homepage": "https://github.com/thewilkybarkid/hyper-ts-oauth", |
10370
141