0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-23 20:33:16 -05:00

Merge pull request #968 from logto-io/sijie-alipay-code-json

feat(connector-alipay): parse code from json
This commit is contained in:
Wang Sijie 2022-05-27 12:39:16 +08:00 committed by GitHub
commit 6b53ed5f4f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View file

@ -41,6 +41,20 @@ import { signingParameters } from './utils';
export type { AlipayNativeConfig } from './types'; export type { AlipayNativeConfig } from './types';
type CodePayload = {
auth_code: string;
};
const parseCodeFromJson = (json: string): string => {
try {
const { auth_code } = JSON.parse(json) as CodePayload;
return auth_code;
} catch {
return json;
}
};
export default class AlipayNativeConnector implements SocialConnector { export default class AlipayNativeConnector implements SocialConnector {
public metadata: ConnectorMetadata = defaultMetadata; public metadata: ConnectorMetadata = defaultMetadata;
@ -72,7 +86,7 @@ export default class AlipayNativeConnector implements SocialConnector {
timestamp: dayjs().format('YYYY-MM-DD HH:mm:ss'), timestamp: dayjs().format('YYYY-MM-DD HH:mm:ss'),
version: '1.0', version: '1.0',
grant_type: 'authorization_code', grant_type: 'authorization_code',
code, code: parseCodeFromJson(code),
charset: 'UTF8', charset: 'UTF8',
...config, ...config,
}; };

View file

@ -35,6 +35,20 @@ import { signingParameters } from './utils';
export type { AlipayConfig } from './types'; export type { AlipayConfig } from './types';
type CodePayload = {
auth_code: string;
};
const parseCodeFromJson = (json: string): string => {
try {
const { auth_code } = JSON.parse(json) as CodePayload;
return auth_code;
} catch {
return json;
}
};
export default class AlipayConnector implements SocialConnector { export default class AlipayConnector implements SocialConnector {
public metadata: ConnectorMetadata = defaultMetadata; public metadata: ConnectorMetadata = defaultMetadata;
@ -73,7 +87,7 @@ export default class AlipayConnector implements SocialConnector {
timestamp: this.getTimestamp(), timestamp: this.getTimestamp(),
version: '1.0', version: '1.0',
grant_type: 'authorization_code', grant_type: 'authorization_code',
code, code: parseCodeFromJson(code),
charset: 'UTF8', charset: 'UTF8',
...config, ...config,
}; };