Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit b73cc7b

Browse files
author
Dom Harrington
committed
Add basic auth authentication type
1 parent 71e9443 commit b73cc7b

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

packages/api-explorer-ui/__tests__/SecurityInput.test.jsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const React = require('react');
2-
const { shallow } = require('enzyme');
2+
const { mount, shallow } = require('enzyme');
33
const SecurityInput = require('../src/SecurityInput');
44

55
describe('oauth2', () => {
@@ -32,3 +32,26 @@ describe('oauth2', () => {
3232
expect(onChange.mock.calls[0][0]).toEqual({ auth: { 'test-auth': '1234' } });
3333
});
3434
});
35+
36+
describe('basic', () => {
37+
const props = { scheme: { type: 'basic', _key: 'test-basic' }, onChange: () => {} };
38+
39+
test('should send auth apiKey into onChange()', () => {
40+
const onChange = jest.fn();
41+
const securityInput = mount(<SecurityInput {...props} onChange={onChange} />);
42+
43+
securityInput.find('input[name="user"]').node.value = 'user';
44+
securityInput.find('input[name="user"]').simulate('change');
45+
securityInput.find('input[name="password"]').node.value = 'pass';
46+
securityInput.find('input[name="password"]').simulate('change');
47+
48+
expect(onChange.mock.calls[1][0]).toEqual({
49+
auth: {
50+
'test-basic': {
51+
user: 'user',
52+
password: 'pass',
53+
},
54+
},
55+
});
56+
});
57+
});

packages/api-explorer-ui/__tests__/lib/configure-security.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('configure-security', () => {
2828
const user = 'user';
2929
const password = 'password';
3030
const values = {
31-
auth: { user, password },
31+
auth: { test: { user, password } },
3232
};
3333

3434
expect(configureSecurity({

packages/api-explorer-ui/src/CodeSample.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,3 @@ CodeSample.propTypes = {
7272
};
7373

7474
module.exports = CodeSample;
75-

packages/api-explorer-ui/src/SecurityInput.jsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,56 @@ Oauth2.defaultProps = {
4444
apiKey: '',
4545
};
4646

47+
class Basic extends React.Component {
48+
constructor(props) {
49+
super(props);
50+
this.state = { user: '', password: '' };
51+
this.inputChange = this.inputChange.bind(this);
52+
}
53+
inputChange(name, value) {
54+
this.setState((previousState) => {
55+
return Object.assign({}, previousState, { [name]: value });
56+
}, () => {
57+
this.props.change(this.state);
58+
});
59+
}
60+
render() {
61+
return (
62+
<div className="row">
63+
<div className="col-xs-6">
64+
<label htmlFor="user">username</label>
65+
<input
66+
type="text"
67+
onChange={e => this.inputChange(e.currentTarget.name, e.currentTarget.value)}
68+
name="user"
69+
/>
70+
</div>
71+
<div className="col-xs-6">
72+
<label htmlFor="password">password</label>
73+
<input
74+
type="text"
75+
onChange={e => this.inputChange(e.currentTarget.name, e.currentTarget.value)}
76+
name="password"
77+
/>
78+
</div>
79+
</div>
80+
);
81+
}
82+
}
83+
84+
Basic.propTypes = {
85+
change: PropTypes.func.isRequired,
86+
};
87+
4788
function SecurityInput(props) {
4889
function change(value) {
4990
return props.onChange({ auth: { [props.scheme._key]: value } });
5091
}
5192
switch (props.scheme.type) {
5293
case 'oauth2':
5394
return Oauth2(Object.assign({}, props, { change }));
95+
case 'basic':
96+
return React.createElement(Basic, Object.assign({}, props, { change }));
5497
default: return <span />;
5598
}
5699
}

packages/api-explorer-ui/src/lib/configure-security.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = function configureSecurity(oas, values, scheme) {
1515
if (security.type === 'basic') {
1616
return harValue('headers', {
1717
name: 'Authorization',
18-
value: `Basic ${new Buffer(`${values.auth.user}:${values.auth.password}`).toString('base64')}`,
18+
value: `Basic ${new Buffer(`${values.auth[key].user}:${values.auth[key].password}`).toString('base64')}`,
1919
});
2020
}
2121

0 commit comments

Comments
 (0)