Skip to content

Commit a5568f9

Browse files
authored
improve: OAuth2 UI and test suite (via #5066)
* create `features` folder * add base oauth2 server * continue implementing OAuth tests * WIP * add password flow tests * modify Password flow credential types * remove query string credential type * add test case for Authorization flow * add specific Authorization value for Password flow test * WIP * fix linter issues
1 parent 91b1bec commit a5568f9

File tree

14 files changed

+504
-55
lines changed

14 files changed

+504
-55
lines changed

package-lock.json

Lines changed: 68 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@
9595
"babel-preset-react": "^6.23.0",
9696
"babel-preset-stage-0": "^6.22.0",
9797
"babel-runtime": "^6.23.0",
98+
"body-parser": "^1.18.3",
9899
"bundlesize": "^0.17.0",
99100
"chromedriver": "^2.38.3",
100101
"copy-webpack-plugin": "^4.0.1",
102+
"cors": "^2.8.4",
101103
"css-loader": "^0.28.11",
102104
"cypress": "^3.1.0",
103105
"dedent": "^0.7.0",
@@ -108,6 +110,7 @@
108110
"eslint-plugin-mocha": "^4.11.0",
109111
"eslint-plugin-react": "^7.10.0",
110112
"expect": "^1.20.2",
113+
"express": "^4.16.4",
111114
"extract-text-webpack-plugin": "^3.0.2",
112115
"file-loader": "^1.1.11",
113116
"git-describe": "^4.0.1",
@@ -124,6 +127,7 @@
124127
"npm-run-all": "^4.1.2",
125128
"null-loader": "0.1.1",
126129
"nyc": "^11.3.0",
130+
"oauth2-server": "^2.4.1",
127131
"open": "0.0.5",
128132
"postcss-loader": "^2.1.5",
129133
"raw-loader": "0.5.1",

src/core/components/auth/oauth2.jsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class Oauth2 extends React.Component {
2424
let username = auth && auth.get("username") || ""
2525
let clientId = auth && auth.get("clientId") || authConfigs.clientId || ""
2626
let clientSecret = auth && auth.get("clientSecret") || authConfigs.clientSecret || ""
27-
let passwordType = auth && auth.get("passwordType") || "request-body"
27+
let passwordType = auth && auth.get("passwordType") || "basic"
2828

2929
this.state = {
3030
appName: authConfigs.appName,
@@ -150,22 +150,21 @@ export default class Oauth2 extends React.Component {
150150
}
151151
</Row>
152152
<Row>
153-
<label htmlFor="password_type">type:</label>
153+
<label htmlFor="password_type">Client credentials location:</label>
154154
{
155155
isAuthorized ? <code> { this.state.passwordType } </code>
156156
: <Col tablet={10} desktop={10}>
157157
<select id="password_type" data-name="passwordType" onChange={ this.onInputChange }>
158+
<option value="basic">Authorization header</option>
158159
<option value="request-body">Request body</option>
159-
<option value="basic">Basic auth</option>
160-
<option value="query">Query parameters</option>
161160
</select>
162161
</Col>
163162
}
164163
</Row>
165164
</Row>
166165
}
167166
{
168-
( flow === APPLICATION || flow === IMPLICIT || flow === ACCESS_CODE || ( flow === PASSWORD && this.state.passwordType!== "basic") ) &&
167+
( flow === APPLICATION || flow === IMPLICIT || flow === ACCESS_CODE || flow === PASSWORD ) &&
169168
( !isAuthorized || isAuthorized && this.state.clientId) && <Row>
170169
<label htmlFor="client_id">client_id:</label>
171170
{
@@ -183,7 +182,7 @@ export default class Oauth2 extends React.Component {
183182
}
184183

185184
{
186-
( flow === APPLICATION || flow === ACCESS_CODE || ( flow === PASSWORD && this.state.passwordType!== "basic") ) && <Row>
185+
( (flow === APPLICATION || flow === ACCESS_CODE || flow === PASSWORD) && <Row>
187186
<label htmlFor="client_secret">client_secret:</label>
188187
{
189188
isAuthorized ? <code> ****** </code>
@@ -197,7 +196,7 @@ export default class Oauth2 extends React.Component {
197196
}
198197

199198
</Row>
200-
}
199+
)}
201200

202201
{
203202
!isAuthorized && scopes && scopes.size ? <div className="scopes">

src/core/components/live-response.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export default class LiveResponse extends React.Component {
8080
</div>
8181
}
8282
<h4>Server response</h4>
83-
<table className="responses-table">
83+
<table className="responses-table live-responses-table">
8484
<thead>
8585
<tr className="responses-header">
8686
<td className="col col_header response-col_status">Code</td>

src/core/plugins/auth/actions.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,28 +74,23 @@ export const authorizePassword = ( auth ) => ( { authActions } ) => {
7474
let { schema, name, username, password, passwordType, clientId, clientSecret } = auth
7575
let form = {
7676
grant_type: "password",
77-
scope: auth.scopes.join(scopeSeparator)
77+
scope: auth.scopes.join(scopeSeparator),
78+
username,
79+
password
7880
}
7981
let query = {}
8082
let headers = {}
8183

82-
if ( passwordType === "basic") {
83-
headers.Authorization = "Basic " + btoa(username + ":" + password)
84-
} else {
85-
Object.assign(form, {username}, {password})
86-
87-
switch ( passwordType ) {
88-
case "query":
89-
setClientIdAndSecret(query, clientId, clientSecret)
90-
break
84+
switch (passwordType) {
85+
case "request-body":
86+
setClientIdAndSecret(form, clientId, clientSecret)
87+
break
9188

92-
case "request-body":
93-
setClientIdAndSecret(form, clientId, clientSecret)
94-
break
95-
96-
default:
97-
headers.Authorization = "Basic " + btoa(clientId + ":" + clientSecret)
98-
}
89+
case "basic":
90+
headers.Authorization = "Basic " + btoa(clientId + ":" + clientSecret)
91+
break
92+
default:
93+
console.warn(`Warning: invalid passwordType ${passwordType} was passed, not including client id and secret`)
9994
}
10095

10196
return authActions.authorizeRequest({ body: buildFormData(form), url: schema.get("tokenUrl"), name, headers, query, auth})
File renamed without changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// from https://github.com/pedroetb/node-oauth2-server-example
2+
3+
var Http = require("http")
4+
var path = require("path")
5+
var express = require("express")
6+
var bodyParser = require("body-parser")
7+
var oauthserver = require("oauth2-server")
8+
var cors = require("cors")
9+
10+
var app = express()
11+
12+
app.use(cors())
13+
14+
app.use(bodyParser.urlencoded({ extended: true }))
15+
16+
app.use(bodyParser.json())
17+
18+
app.oauth = oauthserver({
19+
model: require("./model.js"),
20+
grants: ["password", "client_credentials", "implicit"],
21+
debug: true
22+
})
23+
24+
app.all("/oauth/token", app.oauth.grant())
25+
26+
app.get("/swagger.yaml", function (req, res) {
27+
res.sendFile(path.join(__dirname, "swagger.yaml"))
28+
})
29+
30+
app.get("*", app.oauth.authorise(), function (req, res) {
31+
res.send("Secret secrets are no fun, secret secrets hurt someone.")
32+
})
33+
34+
app.use(app.oauth.errorHandler())
35+
36+
function startServer() {
37+
var httpServer = Http.createServer(app)
38+
httpServer.listen("3231")
39+
40+
return function stopServer() {
41+
httpServer.close()
42+
}
43+
}
44+
45+
module.exports = startServer
46+
47+
if (require.main === module) {
48+
// for debugging
49+
startServer()
50+
}

0 commit comments

Comments
 (0)