Javascript:使用 Salesforce 实施无密码登录

来源:undefined 2025-02-03 03:28:41 1027

salesforce 提供无头无密码登录流程,允许注册用户无缝访问应用程序。无密码登录非常用户友好,它只需要一个有效的电子邮件地址。在这篇文章中,我将分享一些用于使用 salesforce 实现无密码登录流程的代码片段。

要求

开始之前,请确保满足以下条件:

a) 您有权访问 salesforce 环境。

b) 您已注册用户并启用无密码登录选项。

第一步:发送用户名

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

export async function passwordlesslogin(username, captcha) {

const payload = {

username,

recaptcha: captcha,

verificationmethod: "email",

};

const config = {

headers: {

"content-type": "application/json",

},

method: "post",

body: json.stringify(payload),

};

const url = `${replace_with_your_salesforce_cloud_url}/services/auth/headless/init/passwordless/login`;

const response = await fetch(url, config);

const result = await response.json();

return result;

}

登录后复制

这是对 salesforce 的第一次调用。请注意以下事项:

a) 您需要通过验证码。如需帮助,请查看 recaptcha v3。

b) 验证方法设置为电子邮件,这会告诉 salesforce 通过电子邮件发送一次性密码 (otp)。

立即学习Java免费学习笔记(深入)”;

c) 除了验证码和验证方法之外,唯一需要的其他参数是用户名,它对应于用户注册的电子邮件。

如果请求成功,salesforce 将向提供的用户名发送一封电子邮件并返回如下响应:

1

2

3

4

{

"identifier": "mff0rwswmdawmdixdvrk",

"status": "success"

}

登录后复制

第二步:捕获 otp

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

export async function passwordlessauthorize(identifier, code) {

const authorization = btoa(identifier + ":" + code);

const config = {

headers: {

"auth-request-type": "passwordless-login",

"auth-verification-type": "email",

authorization: "basic " + authorization,

"content-type": "application/x-www-form-urlencoded",

},

method: "post",

body: new urlsearchparams({

response_type: "code_credentials",

client_id: "replace_with_your_client_id",

redirect_uri: "replace_with_your_redirect_uri",

}),

};

const response = await fetch(

`${replace_with_your_salesforce_cloud_url}/services/oauth2/authorize`,

config

);

const result = await response.json();

return result;

}

登录后复制

这是第二次致电 salesforce。这里有几个要点:

a) 标识符是第一步返回的值。

b) 该代码是 salesforce 通过电子邮件发送的 otp。

c) 注意 auth 和 authorization 标头的定义方式。

d) content-type 是 application/x-www-form-urlencoded。注意正文如何使用 urlsearchparams 进行格式化。

如果一切顺利,salesforce 将返回如下响应:

1

2

3

4

5

{

"code": "aprxoppu1bwu2d3sbssbklubzop4sxhra2tb.p3lapgviexvmwyigvaf6vtebi7ottvto18uuq==",

"sfdc_community_url": "https://site.com/application",

"sfdc_community_id": "xxxxxxxx"

}

登录后复制

第三步:用代码交换访问令牌

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

export async function getaccesstoken(code) {

const config = {

headers: {

"content-type": "application/x-www-form-urlencoded",

},

method: "post",

body: new urlsearchparams({

code,

grant_type: "authorization_code",

client_id: "replace_with_your_client_id",

redirect_uri: "replace_with_your_redirect_uri",

}),

};

const response = await fetch(

`${replace_with_your_salesforce_cloud_url}/services/oauth2/token`,

config

);

const result = await response.json();

return result;

}

登录后复制

响应应如下所示:

1

2

3

4

5

6

7

8

9

10

11

12

13

{

"access_token": "00dej000006dhsr!aqeaqgpj5xvnbl1qq8pi4xjyghmxajig7ca4ci0mixzcg7ho_yyzanyxpx9uelaez2905vfne6vzhmavmndoboks.wzhlzhc",

"refresh_token": "5aep861i1ns2kincggjsdz4ootyjzqw_gzds5f1pwqh0nfu0akgldaw5ptc.qadf.bvz1aplukjyise2lxx5kq0",

"sfdc_community_url": "https://site.com/application",

"sfdc_community_id": "xxxxxxxx",

"signature": "jwnfzy2g3phxcl3fjrfju5x2ayxw7ozsfg2bz6bbb74=",

"scope": "refresh_token openid user_registration_api api",

"id_token": "...",

"instance_url": "https://site.com/",

"id": "https://test.salesforce.com/id/00000/11111",

"token_type": "bearer",

"issued_at": "1733700157803"

}

登录后复制

确保安全地存储 access_token,从这里,您可以为您的用户创建会话。出于安全考虑,最好在服务器上执行这些方法。

id_token 是 jwt 令牌。如果你解码它,它会看起来像这样:

1

2

3

4

5

6

7

8

{

"at_hash": "HTa4VEmQhCYi59WLhiL6DQ",

"sub": "https://test.salesforce.com/id/00000/11111",

"aud": "3MXG9j6uMOMC1DNjcltNj9xPoUi7xNbiSwPqOjmDSLfCW54f_Qf6EG3EKqUAGT6xyGPc7jqAMi4ZRw8WTIf9B",

"iss": "https://site.com/",

"exp": 1733702662,

"iat": 1733702542

}

登录后复制

您还可以自定义 jwt 以包含其他数据。但是,建议保持结构最小化,并根据需要使用访问令牌来获取其他信息。

结论

无密码登录对每个人来说都很方便,大多数云服务(例如 salesforce)现在都提供无密码登录流程。利用此功能可以简化登录过程并改善用户体验。

以上就是Javascript:使用 Salesforce 实施无密码登录的详细内容,更多请关注php中文网其它相关文章!

最新文章