REFACTOR: Ouath

This commit is contained in:
Mark Silva 2023-04-25 16:37:50 +08:00
parent 223ee4bab5
commit dff2eb10c5
5 changed files with 44 additions and 11 deletions

View File

@ -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<API.Any>(clientConfig, Any)(GetAny)("/users/current"));
})();

View File

@ -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": {

View File

@ -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<any> => {
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
);
};

View File

@ -51,3 +51,4 @@ export {
get,
create,
} from "./api/client";
export { OAuth, OAuthConfig } from "./oauth";

View File

@ -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"