简体   繁体   English

OpenID Connect,oAuth2-从哪里开始?

[英]OpenID Connect, oAuth2 - Where to start?

I am not sure which approach I should be taking in our implementation and need some guidance. 我不确定在实施过程中应该采用哪种方法,是否需要一些指导。

I have a REST API (api.mysite.com) built in the Yii2 Framework (PHP) that accesses data from mysite.com (database). 我有一个Yii2框架(PHP)内置的REST API(api.mysite.com),可从mysite.com(数据库)访问数据。 On mysite.com our users will be able to create Connected Apps that will provision a client id + secret - granting access to their account (full scope?). 在mysite.com上,我们的用户将能够创建将提供客户ID +机密的关联应用-授予对其帐户的访问权限(全部权限?)。

Based on my research, the next step seems to be setting up something to actually provide the bearer tokens to be passed to the api - I have been leaning towards oAuth2, but then I read that oAuth2 does not provide authentication. 根据我的研究,下一步似乎是设置一些东西来实际提供要传递给api的承载令牌-我一直倾向于使用oAuth2,但随后我看到oAuth2不提供身份验证。 Based on this I think I need OpenID Connect in order to also provide user tokens because my API needs to restrict data based on the user context. 基于此,我想需要OpenID Connect才能提供用户令牌,因为我的API需要根据用户上下文限制数据。

In this approach, it is my understanding that I need to have an Authentication Server - so a few questions: 通过这种方式,据我了解,我需要拥有一个身份验证服务器-几个问题:

  • Is there software I can install to act as an OpenID Connect/oAuth2 authentication server? 我可以安装用作OpenID Connect / oAuth2身份验证服务器的软件吗?

  • Are there specific Amazon Web Services that will act as an OpenID Connect/oAuth2 Authentication Server? 是否有将用作OpenID Connect / oAuth2身份验证服务器的特定Amazon Web Services?

  • I am assuming the flow will be: App makes a request to the auth server with client id + secret and receives an access token. 我假设流程将是:App向具有客户端ID +机密的身份验证服务器发出请求,并接收访问令牌。 Access token can be used to make API calls. 访问令牌可用于进行API调用。 Where are these tokens stored (I am assuming a database specific to the service/software I am using?) 这些令牌存储在哪里(我假设一个特定于我正在使用的服务/软件的数据库?)

  • When making API calls would I pass a bearer token AND a user token? 进行API调用时,我会传递承载令牌和用户令牌吗?

Any insight is greatly appreciated. 非常感谢任何见解。

your understanding is not very far from reality. 您的理解与现实相差不远。 Imagine you have two servers one for Authentication, this one is responsible for generating the tokens based on a Authorization Basic and base64 encoded CLientID / ClientSecret combo. 假设您有两台服务器用于身份验证,这台服务器负责根据Authorization Basic和base64编码的CLientID / ClientSecret组合生成令牌。 This is application authentication basically. 这基本上是应用程序认证。 If you want to add user data as well, simply pass username / password in the post body, authenticate on the server side and then add some more data to the tokens, like the usernames, claims, roles, etc 如果您还想添加用户数据,只需在帖子正文中传递用户名/密码,在服务器端进行身份验证,然后向令牌中添加更多数据,例如用户名,声明,角色等。

You can control what you put in these tokens, if you use something like JWT ( Json Web Tokens ) then they are simply json bits of data. 您可以控制这些令牌中的内容,如果使用JWT之类的东西(Json Web令牌),则它们只是数据的json位。

then you have a Resource server, you hit it with a Authorization Bearer and the token you obtained from the Authorization one. 那么您就拥有了一个资源服务器,并用授权载体和从授权服务器获得的令牌对其进行了攻击。

Initially the tokens are not stored anywhere, they are issued for a period of time you control. 最初,令牌不会存储在任何地方,而是在您控制的一段时间内发行。 You can however do something else and store them in a db if you really want to. 但是,如果您确实愿意,可以执行其他操作,并将它们存储在数据库中。 The expiration is much safer though, even if someone gets their hands on them they won't be available for long! 即使到期,即使有人把手放在他们的身上,到期也要安全得多! In my case I used 30 minutes for token validity. 就我而言,我花了30分钟来确保令牌有效。

Now, you haven't specified what languages/frameworks you are looking into. 现在,您尚未指定要使用的语言/框架。 If you use something like dot net then look into IdentityServer, version 4 is for Dot net core, 3 for anything below. 如果您使用点网之类的东西,请查看IdentityServer,版本4适用于点网核心,版本3适用于以下内容。

I also have a pretty long article on this subject if you are interested: 如果您对此感兴趣,我也有一篇很长的文章:

https://eidand.com/2015/03/28/authorization-system-with-owin-web-api-json-web-tokens/ https://eidand.com/2015/03/28/authorization-system-with-owin-web-api-json-web-tokens/

Hopefully all this clarifies some of the questions you have. 希望所有这些可以澄清您的一些问题。

-- Added to answer a question in comments. -添加以回答评论中的问题。

The tokens contain all the information they need to be authenticated by the resource server correctly, you don't need to store them in a database for that. 令牌包含它们需要由资源服务器正确验证的所有信息,因此您无需将其存储在数据库中。 As I already said, you can store them but in my mind this makes them less secure. 正如我已经说过的,您可以存储它们,但是在我看来,这会使它们的安全性降低。 Don't forget you control what goes into a token so you can add usernames if that's what you need. 不要忘记控制令牌中的内容,因此可以在需要时添加用户名。

Imagine this scenario, you want to authenticate the application and the user in the same call to the Authorization Server. 想象一下这种情况,您想在对授权服务器的同一调用中对应用程序和用户进行身份验证。 Do the OAuth2 in the standard way, which means authenticate the application first based on the client id / client secret. 以标准方式执行OAuth2,这意味着首先根据客户端ID /客户端密码对应用程序进行身份验证。 If that passes then next do the user authentication. 如果通过了,则接下来进行用户身份验证。 Add the username or userid to the token you generate and any other bits of information you need. 将用户名或用户ID添加到您生成的令牌以及所需的其他信息中。 What this means that the resource server can safely assume that the username passed to it in the token has already been validated by the authentication server otherwise no token would have been generated in the the first place. 这意味着资源服务器可以安全地假定令牌中传递给它的用户名已由身份验证服务器验证,否则首先不会生成令牌。

I prefer to keep these two separate myself, meaning let the AS ( Authorization Server) to deal with the application level security. 我宁愿将这两部分分开保存,这意味着让AS(授权服务器)处理应用程序级别的安全性。 Then on the RS (Resource Server) side you have an endpoint point like ValidateUser for example, which takes care of the user validation, after which you can do whatever you need. 然后在RS(资源服务器)端,您有一个诸如ValidateUser之类的端点,它负责用户验证,之后您可以执行所需的任何操作。 Pick whichever feels more appropriate for your project I'd say. 选择我认为适合您的项目的任何一种。

One final point, ALWAYS make sure all your api calls ( both AS and RS are just apis really ) are made over HTTPS and never ever have any important information transmitted via a GET call which means the URL can be intercepted. 最后一点,始终确保您的所有api调用(实际上AS和RS都只是api)都是通过HTTPS进行的,并且永远不会通过GET调用传输任何重要信息,这意味着可以拦截URL。 Both Headers and POST body are encrypted and secure over HTTPS. 标头和POST正文均已加密,并通过HTTPS进行安全保护。

This should address both your questions, I believe. 我相信,这应该解决您的两个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM