-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Authorization Code Flow for Single Page Applications: Implementation of the Fetch Client in the Browser #1144
Merged
pkanher617
merged 14 commits into
authorization-code-flow-base
from
authorization-code-flow-browser-fetch
Dec 12, 2019
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
850222b
Implementing fetch client, intializing the browser storage class
pkanher617 0aa8698
Merge branch 'authorization-code-flow-browser-crypto' into authorizat…
pkanher617 19699f1
Adding changes requested from review
pkanher617 6588d77
Adding configuration option for network client
pkanher617 a69fc5d
Merge branch 'authorization-code-flow-browser-crypto' into authorizat…
pkanher617 ae8bd06
Merge branch 'authorization-code-flow-browser-crypto' into authorizat…
pkanher617 43342bd
Merge branch 'authorization-code-flow-browser-crypto' into authorizat…
1db1dbe
abstracting network APIs
30bbb55
Adding xhr client
e8cea84
Merge branch 'authorization-code-flow-browser-crypto' into authorizat…
db6b111
Adding updates from review
e540784
adding object for optional request params
045997d
Merge branch 'authorization-code-flow-browser-crypto' into authorizat…
0b368c5
Update XhrClient.ts
pkanher617 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
import { INetworkModule, NetworkRequestOptions } from "msal-common"; | ||
|
||
enum HTTP_REQUEST_TYPE { | ||
GET = "GET", | ||
POST = "POST" | ||
}; | ||
|
||
export class FetchClient implements INetworkModule { | ||
|
||
/** | ||
* Fetch Client for REST endpoints - Get request | ||
* @param url | ||
* @param headers | ||
* @param body | ||
*/ | ||
async sendGetRequestAsync<T>(url: string, options?: NetworkRequestOptions): Promise<T> { | ||
const response = await fetch(url, { | ||
method: HTTP_REQUEST_TYPE.GET, | ||
headers: this.getFetchHeaders(options.headers), | ||
credentials: "include", | ||
body: options.body | ||
}); | ||
return await response.json() as T; | ||
} | ||
|
||
/** | ||
* Fetch Client for REST endpoints - Post request | ||
* @param url | ||
* @param headers | ||
* @param body | ||
*/ | ||
async sendPostRequestAsync<T>(url: string, options?: NetworkRequestOptions): Promise<T> { | ||
const response = await fetch(url, { | ||
method: HTTP_REQUEST_TYPE.POST, | ||
headers: this.getFetchHeaders(options.headers), | ||
credentials: "include", | ||
body: options.body | ||
}); | ||
return await response.json() as T; | ||
} | ||
|
||
/** | ||
* Get Fetch API Headers object from string map | ||
* @param inputHeaders | ||
*/ | ||
private getFetchHeaders(inputHeaders: Map<string, string>): Headers { | ||
if (!inputHeaders) { | ||
return null; | ||
} | ||
const headers = new Headers; | ||
for (const headerName in inputHeaders.keys()) { | ||
headers.append(headerName, inputHeaders.get(headerName)); | ||
} | ||
return headers; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
import { INetworkModule, NetworkRequestOptions } from "msal-common"; | ||
|
||
export class XhrClient implements INetworkModule { | ||
|
||
async sendGetRequestAsync<T>(url: string, options?: NetworkRequestOptions): Promise<T> { | ||
return this.sendRequestAsync(url, "GET", options); | ||
} | ||
|
||
async sendPostRequestAsync<T>(url: string, options?: NetworkRequestOptions): Promise<T> { | ||
return this.sendRequestAsync(url, "POST", options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use the enum here also? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated in PR #1149. |
||
} | ||
|
||
private sendRequestAsync<T>(url: string, method: string, options?: NetworkRequestOptions): Promise<T> { | ||
return new Promise<T>((resolve, reject) => { | ||
const xhr = new XMLHttpRequest(); | ||
xhr.open(method, url, /* async: */ true); | ||
this.setXhrHeaders(xhr, options.headers); | ||
xhr.onload = (ev) => { | ||
if (xhr.status < 200 || xhr.status >= 300) { | ||
reject(this.handleError(xhr.responseText)); | ||
} | ||
try { | ||
let jsonResponse = JSON.parse(xhr.responseText) as T; | ||
resolve(jsonResponse); | ||
} catch (e) { | ||
reject(this.handleError(xhr.responseText)); | ||
} | ||
}; | ||
|
||
xhr.onerror = (ev) => { | ||
reject(xhr.status); | ||
}; | ||
|
||
if (method === "GET" || method === "POST") { | ||
xhr.send(options.body); | ||
} | ||
else { | ||
throw "not implemented"; | ||
} | ||
}); | ||
} | ||
|
||
private handleError(responseText: string): any { | ||
let jsonResponse; | ||
try { | ||
jsonResponse = JSON.parse(responseText); | ||
if (jsonResponse.error) { | ||
return jsonResponse.error; | ||
} else { | ||
throw responseText; | ||
} | ||
} catch (e) { | ||
return responseText; | ||
} | ||
} | ||
|
||
private setXhrHeaders(xhr: XMLHttpRequest, headers: Map<string, string>): void { | ||
for (const headerName in headers.keys()) { | ||
xhr.setRequestHeader(headerName, headers.get(headerName)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥 🔥 🔥 🚀