简体   繁体   English

AngularJS和Oauth2提供程序的身份验证?

[英]AngularJS and authentication to an Oauth2 Provider?

We have an API, Oauth2 Provider. 我们有一个API,Oauth2提供商。

From AngularJS client Side app, how can I implement the flow of authentication to this api to get the access token for future requests? 从AngularJS客户端应用程序,我如何实现对此API的身份验证流程以获取未来请求的访问令牌?

What I am searching for is a Implicit Grant flow for this. 我正在寻找的是一个Implicit Grant流程。

I'll provide a 我会提供一个

{
    client-id: "abcdefghijklmnopqrstuvqxyz0123456789", 
    redirect_url: "http:localhost:8080/provider_cb",
    response_type: "token"
}

I have a Rails REST API in the backend and doorkeeper/devise for Oauth2 provision. 我在后端有一个Rails REST API,为Oauth2提供了一个门卫/设计。

I came across angular-oauth , which seems to solve the problem to certain extent. 我遇到了角度oauth ,似乎在一定程度上解决了这个问题。 but, 但,

  1. I do not wish to provide a token verification function (Is this mandatory) 我不想提供令牌验证功能(这是强制性的)
  2. I do not wish to open a new window popup for the login (Wish to do redirections in same window) 我不想为登录打开一个新的窗口弹出窗口(希望在同一个窗口中进行重定向)

Now, 现在,

Q1. Q1。 What I do not understand is how is the whole process started, I can't make any $http request, this returns with a SignIn HTML page. 我不明白的是整个过程是如何开始的,我不能发出任何$ http请求,这会返回一个SignIn HTML页面。 Should I use $location service to redirect to it to login page? 我应该使用$ location服务重定向到登录页面吗? Or should I have a link to the whole GET request to /oauth/authorize?... 或者我应该有一个指向/ oauth / authorize的整个GET请求的链接吗?...

Q2, How will I capture the redirect after SignIn to extract out the access_token? Q2,如何在SignIn之后捕获重定向以提取access_token?

Q3. Q3。 Do know any Service which takes care of Oauth2 authentication or a standard Angular way of doing this? 知道任何负责Oauth2身份验证的服务或标准的Angular方式吗?

Lets try an answer to your three questions: 让我们试着回答你的三个问题:

Q1 ) You should basically understand the general OAuth2 process. Q1 )您应该基本了解一般的OAuth2流程。 It is not an AngularJS-specific implementation you're looking at. 它不是您正在研究的特定于AngularJS的实现。 If you don't want to work with a popup window for the authorization, you'll have to trick around a bit to have the redirect_url / state working correctly after coming back (if you want to have #state - saving - otherwise just specify your entry - url in the redirect_uri). 如果您不想使用弹出窗口进行授权,那么在返回之后您必须耍一下才能使redirect_url /状态正常工作(如果您想要#state - 保存 - 否则只需指定你的条目 - redirect_uri中的网址)。 You should not use $http for the redirection as this is just for XHR and similar calls useful. 您不应该使用$ http进行重定向,因为这仅适用于XHR和类似的调用。 To get your user to the login page, just do a $window.location.href = ' http://yourlogin.com/oauthloginpage '; 要让您的用户访问登录页面,只需执行$ window.location.href =' http://yourlogin.com/oauthloginpage ';

Your app will then redirect to the login page - don't forget your parameters for redirect_url. 然后,您的应用将重定向到登录页面 - 请不要忘记redirect_url的参数。 Also specify other parameters within the request like "scope" / "state" if required. 如果需要,还可以在请求中指定其他参数,如“scope”/“state”。

Q2 ) The redirect AFTER Login will redirect to the uri you specified within your redirect_url. Q2 )重定向AFTER登录将重定向到您在redirect_url中指定的uri。 Then it will come up with something like http://myapp.com/#access_token=foobar&refresh_token=foobar2&state=loremipsem 然后会出现类似http://myapp.com/#access_token=foobar&refresh_token=foobar2&state=loremipsem的内容

Just grab the parts from angular with a bit of code like this: 只需使用以下代码获取角度的部分:

var currentURL = $location.absUrl();
var paramPartOfURL = currentURL.slice(currentURL.indexOf('#') + 1);

Then parse the paramPart into an array with split('&') and iterate through it by looking for the "key" access_token and - voila - there is your access_token ready for being taken into your local storage / service / cookie. 然后将paramPart解析为带有split('&')的数组,并通过查找“key”access_token来迭代它,并且 - 瞧 - 您的access_token已准备好被带入本地存储/服务/ cookie。

There are other implementations you might use to split URLs into parts - maybe there is a better one, but this here would be the "fast shot". 您可以使用其他实现将URL拆分为部分 - 可能有更好的部分,但这里将是“快速镜头”。 After parsing, you can redirect the user again to the correct state with a normal $location.path(....) call and eliminate the # parameters of OAuth2. 解析后,您可以使用正常的$ location.path(....)调用将用户再次重定向到正确的状态,并消除OAuth2的#参数。

Q3 ) If you want to have a way of mapping the source state / route of your AngularJS-app - try misusing the state parameter if your OAuth2-Server implements it. Q3 )如果你想要一种映射AngularJS-app的源状态/路由的方法 - 如果你的OAuth2-Server实现了它,请尝试滥用state参数。 This will survive the request <-> response. 这将在请求< - >响应中存活。 For a non-popup-implementation, I don't know any further module currently. 对于非弹出式实现,我目前不知道任何其他模块。

Another way to play with the OAuth2 stuff is to implement the loginpage on your own and then just interact with a oauth2 provider reacting on rest calls with Basic Auth or other methods. 使用OAuth2的另一种方法是自己实现登录页面,然后与oauth2提供程序进行交互,使用Basic Auth或其他方法对休息调用做出反应。 Then you can use $http calls probably. 然后你可以使用$ http调用。

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

相关问题 实施 mailchimp oauth2 身份验证 - Implement mailchimp oauth2 authentication Rails 3.构建oauth2提供者 - Rails 3. Building an oauth2 provider Rails中的Facebook身份验证期间发生OAuth2错误 - OAuth2 Error during facebook authentication in Rails 使用AngularJS进行Google OAuth2 Devise登录 - Google OAuth2 Devise Sign-in with AngularJS Oauth2提供商(门卫?)是Oauth2消费者(Devise + OmniAuth)的登录用户 - Oauth2 provider (Doorkeeper?) being an Oauth2 consumer (Devise+OmniAuth) for login 如何使用OAuth2提供者Mongoid ORM制作Rails 4 API? - How to make a Rails 4 API with Mongoid ORM, an OAuth2 Provider? Geails for OAuth2 Rails 2.3.5中的消费者和提供者功能 - Gem for oAuth2 Consumer AND Provider functionality in Rails 2.3.5 如何在网守(oauth2)中执行仅应用程序身份验证 - How to do Application-only authentication in doorkeeper (oauth2) 使用Devise,Doorkeeper和OAuth2令牌进行API和应用程序身份验证 - API and Application authentication using Devise, Doorkeeper and OAuth2 token Linkedin Omniauth gem 'omniauth-linkedin-oauth2' 验证失败:invalid_credentials: OAuth2::Error - Linkedin Omniauth gem 'omniauth-linkedin-oauth2' giving Authentication failure! invalid_credentials: OAuth2::Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM