diff --git a/examples/any.ts b/examples/any.ts index 7838b8c..18e7c66 100644 --- a/examples/any.ts +++ b/examples/any.ts @@ -7,16 +7,29 @@ import { Any, GetAny, } from "../src/api"; +import { OAuthConfig } from "../src/oauth"; -const directLogin: DirectLoginAuthentication = { - username: process.env.OBP_USERNAME, - password: process.env.OBP_PASSWORD, - consumerKey: process.env.OBP_CONSUMER_KEY, +//const directLogin: DirectLoginAuthentication = { +// username: process.env.OBP_USERNAME, +// password: process.env.OBP_PASSWORD, +// consumerKey: process.env.OBP_CONSUMER_KEY, +//}; +const oauthConfig: OAuthConfig = { + baseUri: "https://apisandbox.openbankproject.com", + consumerKey: "qhpewipvmnm3ivjk4eoanjauzac34hm0hlec3tct", + consumerSecret: "wshyzpqydxlk2xsckcam3ioo3vbotbrh4ow4v5un", + redirectUrl: "http://127.0.0.1:8085/callback", + accessToken: { + key: "2OEVAOCIT5MJPJ4JSAR5JSNB0AQUOV5YETWSE4YJ", + secret: "D30UEBJE4QVO0ZD3LILB2VRTHRM4CPM2QKGIT2VM", + }, }; + const clientConfig: APIClientConfig = { - baseUri: "https://obp-apisandbox.joinfincubator.com", + baseUri: "https://apisandbox.openbankproject.com", version: Version.v510, - authentication: directLogin, + //authentication: directLogin, + oauthConfig: oauthConfig, }; (async () => { @@ -26,4 +39,6 @@ const clientConfig: APIClientConfig = { "/resource-docs/v5.1.0/obp?tags=Account" ) ); + // Get current user login + console.log(await get(clientConfig, Any)(GetAny)("/users/current")); })(); diff --git a/package.json b/package.json index 0399c87..78804b0 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "dotenv": "^16.0.3", "global": "^4.4.0", "multer": "^1.4.5-lts.1", + "oauth": "^0.10.0", "superagent": "^8.0.9" }, "devDependencies": { diff --git a/src/api/client.ts b/src/api/client.ts index c2a8d25..96f666b 100644 --- a/src/api/client.ts +++ b/src/api/client.ts @@ -24,6 +24,7 @@ */ import superagent from "superagent"; +import { OAuth, OAuthConfig } from "../oauth"; /** * OBP API Versions. @@ -73,7 +74,7 @@ export type DirectLoginAuthentication = { * * @Property {string} baseUri * @Property {Version} version - * @Property {DirectLoginAuthentication} authentication + * @Property {DirectLoginAuthentication} [authentication] * @Property {string} [token] * * @see {@link Version} @@ -84,7 +85,8 @@ export type DirectLoginAuthentication = { export type APIClientConfig = { baseUri: string; version: Version; - authentication: DirectLoginAuthentication; + authentication?: DirectLoginAuthentication; + oauthConfig?: OAuthConfig; token?: string; }; @@ -256,11 +258,20 @@ export const getRequest = async ( path: string ): Promise => { const pathUri = uri(config, path); - if (!config.token) { - config.token = await getDirectLoginToken(config); + let header: any; + if (config.oauthConfig) { + if (!config.oauthConfig.baseUri) + config.oauthConfig["baseUri"] = config.baseUri; + const oauth = new OAuth(config.oauthConfig); + header = oauth.authHeader(pathUri, "GET"); + } else { + if (!config.token) { + config.token = await getDirectLoginToken(config); + header = config.token; + } } return JSON.parse( - (await superagent.get(pathUri).set("Authorization", config.token)).text + (await superagent.get(pathUri).set("Authorization", header)).text ); }; diff --git a/src/index.ts b/src/index.ts index a9a6cbf..586d675 100644 --- a/src/index.ts +++ b/src/index.ts @@ -51,3 +51,4 @@ export { get, create, } from "./api/client"; +export { OAuth, OAuthConfig } from "./oauth"; diff --git a/yarn.lock b/yarn.lock index a5bc2bb..9a22171 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2679,6 +2679,11 @@ npm-run-path@^4.0.1: dependencies: path-key "^3.0.0" +oauth@^0.10.0: + version "0.10.0" + resolved "https://registry.yarnpkg.com/oauth/-/oauth-0.10.0.tgz#3551c4c9b95c53ea437e1e21e46b649482339c58" + integrity sha512-1orQ9MT1vHFGQxhuy7E/0gECD3fd2fCC+PIX+/jgmU/gI3EpRocXtmtvxCO5x3WZ443FLTLFWNDjl5MPJf9u+Q== + object-assign@^4.1.1: version "4.1.1" resolved "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz"