前言

前面已经解释了两个案例了,通信原理其实已经很清楚了,那么纯js客户端是怎么处理的呢?

正文

直接贴例子哈。

https://github.com/IdentityServer/IdentityServer4/tree/main/samples/Quickstarts/4_JavaScriptClient

那么解释一下其实怎么做的吧。

那么就直接来看这个javascriptClient 是怎么实现的吧。

public void Configure(IApplicationBuilder app)
{
	app.UseDefaultFiles();
	app.UseStaticFiles();
}

这个两个就是给静态文件设置路由,这个不过多介绍,.net core 系列已经解读过其源码了。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <button id="login">Login</button>
    <button id="api">Call API</button>
    <button id="logout">Logout</button>

    <pre id="results"></pre>

    <script src="oidc-client.js"></script>
    <script src="app.js"></script>
</body>
</html>

这个的话,那么引入了oidc-client.js 和 app.js。

app.js 肯定就是自己项目的代码了。

oidc-client 可以看下官方介绍。https://github.com/IdentityModel/oidc-client-js

也就是说用好了oidc-client就可以了。
首先实例了一个客户端:

var config = {
    authority: "https://localhost:5001",
    client_id: "js",
    redirect_uri: "https://localhost:5003/callback.html",
    response_type: "code",
    scope:"openid profile api1",
    post_logout_redirect_uri : "https://localhost:5003/index.html",
};
var mgr = new Oidc.UserManager(config);

然后登录的时候:

function login() {
    mgr.signinRedirect();
}

这里面根据我们前面的例子,也能猜到其实就是去查idenityserver的wellknown 跳转到登录地址。

然后identityServer 注册好jsClient 就行。

// JavaScript Client
new Client
{
	ClientId = "js",
	ClientName = "JavaScript Client",
	AllowedGrantTypes = GrantTypes.Code,
	RequireClientSecret = false,

	RedirectUris =           { "https://localhost:5003/callback.html" },
	PostLogoutRedirectUris = { "https://localhost:5003/index.html" },
	AllowedCorsOrigins =     { "https://localhost:5003" },

	AllowedScopes =
	{
		IdentityServerConstants.StandardScopes.OpenId,
		IdentityServerConstants.StandardScopes.Profile,
		"api1"
	}
}

下面直接看效果就好了。

首先去拿identityserver 的公开信息。

identity4 系列————纯js客户端案例篇[四]-LMLPHP

然后访问identityServer:

identity4 系列————纯js客户端案例篇[四]-LMLPHP

identity4 系列————纯js客户端案例篇[四]-LMLPHP

然后就到了登录页面了。

然后进行登录。

identity4 系列————纯js客户端案例篇[四]-LMLPHP

还是和以前一样,这里可以直接运行例子更加直观。

然后看下客户端拿到授权码后如何获取token的。

identity4 系列————纯js客户端案例篇[四]-LMLPHP

然后通过token 获取到了userinfo。

这里解释一下这里有几个token,一个是access_token 这个是来表示授权令牌,比如访问一些接口的api。

然后这个id_token 是身份令牌,在线解析一下哈:

identity4 系列————纯js客户端案例篇[四]-LMLPHP

那么这个身份令牌是用来干什么的。授权令牌用来访问api授权的,那么身份令牌用来干什么的呢?

这样我们不要猜,直接看官方怎么说。

https://auth0.com/docs/secure/tokens/id-tokens#learn-more

ID tokens are used in token-based authentication to cache user profile information and provide it to a client application, thereby providing better performance and experience. The application receives an ID token after a user successfully authenticates, then consumes the ID token and extracts user information from it, which it can then use to personalize the user's experience.

上面说id tokens 是基于认证token 来缓存用户的信息提供给客户端,提供更好的性能和体验。在认证后应用收到id token,使用id token 收集用户的信息,可以被使用与私有化的用户体验。

我们知道auth2.0 管理授权,但是不管理用户信息。当然了auth 2.0 可以通过api 获取到用户信息,但是这是另外一回事。

比如说我们访问简书,跳转到qq登录,然后又登录回来,这其中包括了认证和授权。认证应该返回id tokens,来证明这个用户认证了。授权应该返回access_token 表示授权token。

然后文档中也给了例子。

For example, suppose you have a regular web app that you register it with Auth0 and configure to allow users to login with Google. Once a user logs in, use the ID token to gather information such as name and email address, which you can then use to auto-generate and send a personalized welcome email.

比如说,假设你有一个常规的web app,这个app 你允许用户他能给个google 登录。 一但用户登录,使用这个id token 可以收集用户信息,比如说名字邮件,这样你就可以给这个用户发送邮件。

上面那些id_token 中好像没有什么东西,那么其实我们可以增加一些东西。

identity4 系列————纯js客户端案例篇[四]-LMLPHP

全部可以看这个:

https://docs.authing.cn/v2/concepts/id-token.html

那么作为客户端下次访问的时候如何去读取数据呢?

identity4 系列————纯js客户端案例篇[四]-LMLPHP

identity4 系列————纯js客户端案例篇[四]-LMLPHP

也就是从下面这里去读数据:

identity4 系列————纯js客户端案例篇[四]-LMLPHP

当然里面还会去执行是否会话过期。

identity4 系列————纯js客户端案例篇[四]-LMLPHP

这里面就是检查会话是否过期。

这个有什么用呢?

identity4 系列————纯js客户端案例篇[四]-LMLPHP

假如我们在identity server 中退出了登录,那么会发生什么呢?

下面是我identity server 退出后的结果:

identity4 系列————纯js客户端案例篇[四]-LMLPHP

那么就会访问https://localhost:5001/connect/authorize。

这里就不看细节了,讲一下效果。

cookie 清除了,会话结束了。

identity4 系列————纯js客户端案例篇[四]-LMLPHP

前面是有identity 会话的。

identity4 系列————纯js客户端案例篇[四]-LMLPHP

那么这cookie 有什么用呢?

identity4 系列————纯js客户端案例篇[四]-LMLPHP

如果cookie 在的话,那么会把cookie带上去访问identity server,identity 如果确定这个会话没有过期,那么不会进入登录界面,而是直接callback 回来。

现在都是不带数据库的,后面把数据库例子说明一下。

08-28 12:35