Skip to content

Commit 61c5979

Browse files
authored
Merge pull request #87 from acjh/patch-2
Add swagger authentication helpers
2 parents 7b29507 + be8c260 commit 61c5979

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

aspnet-core/src/AbpCompanyName.AbpProjectName.Web.Host/Startup/Startup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
107107
// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
108108
app.UseSwaggerUI(options =>
109109
{
110+
options.InjectOnCompleteJavaScript("/swagger/ui/abp.js");
111+
options.InjectOnCompleteJavaScript("/swagger/ui/on-complete.js");
110112
options.SwaggerEndpoint("/swagger/v1/swagger.json", "AbpProjectName API V1");
111113
}); //URL: /swagger
112114
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
var abp = abp || {};
2+
(function () {
3+
4+
/* Application paths *****************************************/
5+
6+
//Current application root path (including virtual directory if exists).
7+
abp.appPath = abp.appPath || '/';
8+
9+
/* AUTHORIZATION **********************************************/
10+
//Implements Authorization API that simplifies usage of authorization scripts generated by Abp.
11+
12+
abp.auth = abp.auth || {};
13+
14+
abp.auth.tokenCookieName = 'Abp.AuthToken';
15+
abp.auth.tokenHeaderName = 'Authorization';
16+
17+
abp.auth.setToken = function (authToken, expireDate) {
18+
abp.utils.setCookieValue(abp.auth.tokenCookieName, authToken, expireDate, abp.appPath);
19+
};
20+
21+
abp.auth.getToken = function () {
22+
return abp.utils.getCookieValue(abp.auth.tokenCookieName);
23+
}
24+
25+
abp.auth.clearToken = function () {
26+
abp.auth.setToken();
27+
}
28+
29+
/* UTILS ***************************************************/
30+
31+
abp.utils = abp.utils || {};
32+
33+
/**
34+
* Sets a cookie value for given key.
35+
* This is a simple implementation created to be used by ABP.
36+
* Please use a complete cookie library if you need.
37+
* @param {string} key
38+
* @param {string} value
39+
* @param {Date} expireDate (optional). If not specified the cookie will expire at the end of session.
40+
* @param {string} path (optional)
41+
*/
42+
abp.utils.setCookieValue = function (key, value, expireDate, path) {
43+
var cookieValue = encodeURIComponent(key) + '=';
44+
45+
if (value) {
46+
cookieValue = cookieValue + encodeURIComponent(value);
47+
}
48+
49+
if (expireDate) {
50+
cookieValue = cookieValue + "; expires=" + expireDate.toUTCString();
51+
}
52+
53+
if (path) {
54+
cookieValue = cookieValue + "; path=" + path;
55+
}
56+
57+
document.cookie = cookieValue;
58+
};
59+
60+
/**
61+
* Gets a cookie with given key.
62+
* This is a simple implementation created to be used by ABP.
63+
* Please use a complete cookie library if you need.
64+
* @param {string} key
65+
* @returns {string} Cookie value or null
66+
*/
67+
abp.utils.getCookieValue = function (key) {
68+
var equalities = document.cookie.split('; ');
69+
for (var i = 0; i < equalities.length; i++) {
70+
if (!equalities[i]) {
71+
continue;
72+
}
73+
74+
var splitted = equalities[i].split('=');
75+
if (splitted.length != 2) {
76+
continue;
77+
}
78+
79+
if (decodeURIComponent(splitted[0]) === key) {
80+
return decodeURIComponent(splitted[1] || '');
81+
}
82+
}
83+
84+
return null;
85+
};
86+
87+
/**
88+
* Deletes cookie for given key.
89+
* This is a simple implementation created to be used by ABP.
90+
* Please use a complete cookie library if you need.
91+
* @param {string} key
92+
* @param {string} path (optional)
93+
*/
94+
abp.utils.deleteCookie = function (key, path) {
95+
var cookieValue = encodeURIComponent(key) + '=';
96+
97+
cookieValue = cookieValue + "; expires=" + (new Date(new Date().getTime() - 86400000)).toUTCString();
98+
99+
if (path) {
100+
cookieValue = cookieValue + "; path=" + path;
101+
}
102+
103+
document.cookie = cookieValue;
104+
}
105+
106+
/* SECURITY ***************************************/
107+
abp.security = abp.security || {};
108+
abp.security.antiForgery = abp.security.antiForgery || {};
109+
110+
abp.security.antiForgery.tokenCookieName = 'XSRF-TOKEN';
111+
abp.security.antiForgery.tokenHeaderName = 'X-XSRF-TOKEN';
112+
113+
abp.security.antiForgery.getToken = function () {
114+
return abp.utils.getCookieValue(abp.security.antiForgery.tokenCookieName);
115+
};
116+
117+
})();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
var abp = abp || {};
2+
(function () {
3+
4+
/* Swagger */
5+
6+
abp.swagger = abp.swagger || {};
7+
8+
abp.swagger.addAuthToken = function () {
9+
var authToken = abp.auth.getToken();
10+
if (!authToken) {
11+
return false;
12+
}
13+
var cookieAuth = new SwaggerClient.ApiKeyAuthorization(abp.auth.tokenHeaderName, 'Bearer ' + authToken, 'header');
14+
swaggerUi.api.clientAuthorizations.add(abp.auth.tokenHeaderName, cookieAuth);
15+
return true;
16+
}
17+
18+
abp.swagger.addCsrfToken = function () {
19+
var csrfToken = abp.security.antiForgery.getToken();
20+
if (!csrfToken) {
21+
return false;
22+
}
23+
var csrfCookieAuth = new SwaggerClient.ApiKeyAuthorization(abp.security.antiForgery.tokenHeaderName, csrfToken, 'header');
24+
swaggerUi.api.clientAuthorizations.add(abp.security.antiForgery.tokenHeaderName, csrfCookieAuth);
25+
return true;
26+
}
27+
28+
abp.swagger.login = function () {
29+
var tenantId = window.prompt('tenantId');
30+
var usernameOrEmailAddress = window.prompt('usernameOrEmailAddress');
31+
if (!usernameOrEmailAddress) {
32+
return false;
33+
}
34+
var password = window.prompt('password');
35+
var xhr = new XMLHttpRequest();
36+
xhr.onreadystatechange = function () {
37+
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
38+
var responseJSON = JSON.parse(xhr.responseText);
39+
var result = responseJSON.result;
40+
var expireDate = new Date(Date.now() + (result.expireInSeconds * 1000));
41+
abp.auth.setToken(result.accessToken, expireDate);
42+
abp.swagger.addAuthToken();
43+
console.log(true);
44+
}
45+
};
46+
xhr.open('POST', '/api/TokenAuth/Authenticate', true);
47+
xhr.setRequestHeader('Abp.TenantId', tenantId);
48+
xhr.setRequestHeader('Content-type', 'application/json');
49+
xhr.send("{" +
50+
"usernameOrEmailAddress:'" + usernameOrEmailAddress + "'," +
51+
"password:'" + password + "'}"
52+
);
53+
}
54+
55+
})();

0 commit comments

Comments
 (0)