ajax support cors,Add CORS feature to support AJAX use of password grant flow

Hi there,

I am using the password grant flow via an ajax call where the requesting app is not running on the same domain as the OAuth server. Without CORS headers, the oauth password grant flow from a web client posting to the oauth server does not work. I did see some people just globally adding the CORS headers in the oauth server middleware or the fronting apache proxy, e.g.

app.use(function (req, res, next) {

res.header('Access-Control-Allow-Origin', 'http://localhost:8100');

res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');

res.header('Access-Control-Allow-Headers', 'Content-Type');

next();

});

Making CORS part of the client configuration will enable a per client configuration. The only thing that needs to be added is a configuration property to the client like client.addCorsHeader and for the actual header to be returned one could use the client.redirectUri parameter on the client. If the addCorsHeader property is enabled, simply strip off the path from the client redirectUri and return this in the Access-Control-Allow-Origin header with the response.

Et voila the OAuth server would now support CORS multiple different oauth clients that call from different domains using the password grant.

Does that work as a proposal and more importantly would this be a valid use case?

Cheers, Niels

Detailed use case:

OAuth server running on https://oauth.fancydomain.com

Client App running on https://myclient.com

Client App client is configured like:

{

"clientId" : "id123",

"clientSecret" : "some client secret",

"addCorsHeader": true,

"redirectUri" : "https://myclient.com/#/app/login"

}

OAuth server and Client App are from the same developer and the Client App only wants to post following to get an access token for use of some other backend API that is a protected resource server:

POST /oauth/token HTTP/1.1

Host: oauth.fancydomain.com

Cache-Control: no-cache

Content-Type: application/x-www-form-urlencoded

grant_type=password&username=somedude&password=secret&client_id=id123

The response the client is after will look something like this:

Remote Address: oauth.fancydomain.com

Request URL: https://oauth.fancydomain.com/oauth/token

Request Method: POST

Status Code: 200 OK

Access-Control-Allow-Headers: Content-Type

Access-Control-Allow-Methods: GET,PUT,POST,DELETE

Access-Control-Allow-Origin: https://myclient.com

...

Content-Type: application/json; charset=utf-8

{"token_type":"bearer","access_token":"a29fe185e2529917e7c778fb9b748a0912f030af","expires_in":2400}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值