Commit 53094133 authored by Andreas Åkre Solberg's avatar Andreas Åkre Solberg

Implement HTTP fetcher with support for OAuth tokens, also supporting userinfo endpoint

parent 80bb7697
Pipeline #3702 passed with stages
in 2 minutes and 37 seconds
...@@ -10,6 +10,10 @@ const actionCreators = createActions({ ...@@ -10,6 +10,10 @@ const actionCreators = createActions({
}, },
tokens: { tokens: {
save: x => (x) save: x => (x)
},
http: {
start: x => (x),
completed: x => (x)
} }
}); });
......
...@@ -25,11 +25,14 @@ class Component extends PureComponent { ...@@ -25,11 +25,14 @@ class Component extends PureComponent {
updateAPIendpoint() { updateAPIendpoint() {
} }
fetchUserinfo() { fetchUserinfo() {
let endpoint = ""; let endpoint = this.props.serverConfig.userinfo_endpoint;
console.error("Get userinfo from ", endpoint)
this.props.httpRequestStart(endpoint)
} }
fetchCustom() { fetchCustom() {
......
...@@ -55,6 +55,13 @@ class Component extends PureComponent { ...@@ -55,6 +55,13 @@ class Component extends PureComponent {
tokenEndpoint = this.props.serverConfig.token_endpoint tokenEndpoint = this.props.serverConfig.token_endpoint
} }
let userinfoEndpoint = ''
if (this.state.userinfoEndpoint) {
userinfoEndpoint = this.state.userinfoEndpoint
} else if (this.props.serverConfig) {
userinfoEndpoint = this.props.serverConfig.userinfo_endpoint
}
let expanded = !!this.props.serverConfig let expanded = !!this.props.serverConfig
return ( return (
...@@ -70,6 +77,13 @@ class Component extends PureComponent { ...@@ -70,6 +77,13 @@ class Component extends PureComponent {
<FormControl type="text" value={tokenEndpoint} onChange={this.updateFieldHandler.bind(this)("tokenEndpoint")} /> <FormControl type="text" value={tokenEndpoint} onChange={this.updateFieldHandler.bind(this)("tokenEndpoint")} />
</FormGroup> </FormGroup>
<h4>Userinfo Endpoint</h4>
<FormGroup>
<FormControl type="text" value={userinfoEndpoint} onChange={this.updateFieldHandler.bind(this)("userinfo_endpoint")} />
</FormGroup>
<h4>Client ID</h4> <h4>Client ID</h4>
<FormGroup> <FormGroup>
<FormControl type="text" bsSize="large" value={this.state.clientId} onChange={this.updateFieldHandler.bind(this)("clientId")} /> <FormControl type="text" bsSize="large" value={this.state.clientId} onChange={this.updateFieldHandler.bind(this)("clientId")} />
......
...@@ -10,7 +10,7 @@ const mapStateToProps = (state) => ({ ...@@ -10,7 +10,7 @@ const mapStateToProps = (state) => ({
}) })
const mapDispatchToProps = { const mapDispatchToProps = {
httpRequest: API.httpRequest, httpRequestStart: API.httpRequest,
} }
const Controller = connect( const Controller = connect(
......
...@@ -14,21 +14,50 @@ const reducer = handleActions({ ...@@ -14,21 +14,50 @@ const reducer = handleActions({
} }
} }
return { return {
...state,
serverConfig: action.payload serverConfig: action.payload
} }
} }
}, },
serverConfig: { serverConfig: {
save: (state, action) => ({ save: (state, action) => ({
...state,
serverConfig: action.payload, serverConfig: action.payload,
serverError: null serverError: null
}) })
}, },
tokens: { tokens: {
save: (state, action) => ({ save: (state, action) => ({
token: action.payload, ...state,
...state token: action.payload
}) })
},
http: {
start: (state, action) => ({
...state,
http: {
url: action.payload
}
}),
completed: (state, action) => {
console.error(" HTTP COMPLETED,", action)
if (action.error) {
return {
...state,
http: {
url: state.http.url,
error: action.payload.toString()
}
}
}
return {
...state,
http: {
url: state.http.url,
response: action.payload
}
}
}
} }
}, },
{ {
......
...@@ -35,31 +35,43 @@ const API = { ...@@ -35,31 +35,43 @@ const API = {
} }
}, },
"httpRequest": function() { "httpRequest": function(url) {
return dispatch => { return (dispatch, getState) => {
dispatch(actions.discovery.start(null)) dispatch(actions.http.start(url))
var state = getState()
let url = '/.well-known/openid-configuration' if (!state.token || !state.token.access_token) {
return dispatch(actions.http.completed(new Error("Could not perform HTTP request without a token")))
}
let config = {"headers": {}} let config = {"headers": {}}
// config.headers.Authorization = 'Bearer ' + token.access_token config.headers.Authorization = 'Bearer ' + state.token.access_token
// config.headers["original-token"] = token.access_token
config.method = "GET" config.method = "GET"
config.mode = "cors" config.mode = "cors"
console.log("About to fetch", config) console.log("About to fetch", config)
let resp = null
return fetch(url, config).then((response) => { return fetch(url, config).then((response) => {
resp = response
if (response.ok) { if (response.ok) {
return response.json() return response.json()
} }
throw Error(response.statusText) throw Error(response.statusText)
}) })
.then((data) => { .then((data) => {
console.log("DATA", data)
dispatch(actions.discovery.completed(data)) let hdrs = []
resp.headers.forEach((value, key) => {
hdrs.push({key, value})
})
const r = {
body: data,
headers: hdrs
}
dispatch(actions.http.completed(r))
}) })
.catch((err) => { .catch((err) => {
console.error("ERROR Fetching API Discovery", err) console.error("ERROR Fetching API Discovery", err)
dispatch(actions.discovery.completed(err)) dispatch(actions.http.completed(err))
}) })
} }
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment