Open ryanpetm opened 6 years ago
Had some luck in solving (dirty hack ) this issue myself . Working of the recent fix https://github.com/akveo/nebular/issues/716 I was able to get the azure oauth2 code grant flow working by adding the following to my nebular/auth files
@nebular/auth/strategies/oauth2/oauthe2-stategy.js
NbOAuth2AuthStrategy.prototype.requestToken = function (code) {
var _this = this;
var module = 'token';
var url = this.getActionEndpoint(module);
var requireValidToken = this.getOption(module + ".requireValidToken");
var headers = this.buildAuthHeader() || new HttpHeaders();
headers = headers.append('Content-Type', 'application/x-www-form-urlencoded');
return this.http.post(url, this.buildCodeRequestData(code), { headers: headers })
.pipe(map(function (res) {
return new NbAuthResult(true, res, _this.getOption('redirect.success'), [], _this.getOption('defaultMessages'), _this.createToken(res, requireValidToken));
}), catchError(function (res) { return _this.handleResponseError(res); }));
};
NbOAuth2AuthStrategy.prototype.buildCodeRequestData = function (code) {
var params = {
grant_type: this.getOption('token.grantType'),
code: code,
redirect_uri: this.getOption('token.redirectUri'),
client_id: this.getOption('clientId'),
client_secret: this.getOption('clientSecret'), // axure oauth 2 grant flow auth needs client secret //
resource: this.getOption('token.scope'), // axure oauth 2 grant flow auth needs a resource key identifier //
};
//return this.cleanParams(this.addCredentialsToParams(params));
return this.urlEncodeParameters(this.cleanParams(this.addCredentialsToParams(params))); // xml-encode params
};
I recommend you to add an interceptor like this:
@Injectable()
export class LoginRequestInterceptor implements HttpInterceptor {
constructor() {}
intercept(req: HttpRequest<any>, next: HttpHandler) {
if (req.url === 'http://yourserverhere.com/oauth2/token') {
const xhr = req.clone({
headers: req.headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8'),
body: `username=${encodeURIComponent(req.body.email)}&password=${encodeURIComponent(req.body.password)}&grant_type=password&client_id=clientidhere`,
});
return next.handle(xhr);
} else {
return next.handle(req);
}
}
}
Issue type
I'm submitting a ... (check one with "x")
Issue description
Attempting to leverage ngx-admin templates to recover azure acess tokens with Azure OAuth 2.0 code grant flow. https://docs.microsoft.com/en-us/azure/active-directory/develop/v1-protocols-oauth-code
Current behavior:
Plan to use ngx-admin template to qury microsoft graph api To do so i need my app to recover two tokens
{error: "invalid_request",…} correlation_id : "b5cb3397-b008-4bb0-b475-76ea8deab6c7" error : "invalid_request" error_codes : [90014] error_description : "AADSTS90014: The request body must contain the following parameter: 'grant_type'. ↵Trace ID: d74fbb29-bea3-4bca-b663-b09f65713900 ↵Correlation ID: b5cb3397-b008-4bb0-b475-76ea8deab6c7 ↵Timestamp: 2018-11-02 09:18:53Z" timestamp : "2018-11-02 09:18:53Z" trace_id : "d74fbb29-bea3-4bca-b663-b09f65713900"
For a reference here is the post request as recovered from network debugging
POST https://login.microsoftonline.com/xxxxxxxxxxxxxxxxx/oauth2/token?
Stack overflow post point out that the second endpoint expects payload as xml-endoded https://stackoverflow.com/questions/48996804/azure-active-directory-aadsts90014-invalid-request
Expected behavior:
Expect to be able to negotiate the second transaction Is there a option to enable application/x-www-form-urlencoded for the second trasaction Steps to reproduce:
ngx-admin leveraging oauth2
Related code:
oauth2.module.ts
oauth2-login.component.ts
oauth2-callback.component.ts
Other information:
npm, node, OS, Browser
Angular, Nebular