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({
},
tokens: {
save: x => (x)
},
http: {
start: x => (x),
completed: x => (x)
}
});
......
......@@ -25,11 +25,14 @@ class Component extends PureComponent {
updateAPIendpoint() {
}
fetchUserinfo() {
let endpoint = "";
let endpoint = this.props.serverConfig.userinfo_endpoint;
console.error("Get userinfo from ", endpoint)
this.props.httpRequestStart(endpoint)
}
fetchCustom() {
......
......@@ -55,6 +55,13 @@ class Component extends PureComponent {
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
return (
......@@ -70,6 +77,13 @@ class Component extends PureComponent {
<FormControl type="text" value={tokenEndpoint} onChange={this.updateFieldHandler.bind(this)("tokenEndpoint")} />
</FormGroup>
<h4>Userinfo Endpoint</h4>
<FormGroup>
<FormControl type="text" value={userinfoEndpoint} onChange={this.updateFieldHandler.bind(this)("userinfo_endpoint")} />
</FormGroup>
<h4>Client ID</h4>
<FormGroup>
<FormControl type="text" bsSize="large" value={this.state.clientId} onChange={this.updateFieldHandler.bind(this)("clientId")} />
......
......@@ -10,7 +10,7 @@ const mapStateToProps = (state) => ({
})
const mapDispatchToProps = {
httpRequest: API.httpRequest,
httpRequestStart: API.httpRequest,
}
const Controller = connect(
......
......@@ -14,21 +14,50 @@ const reducer = handleActions({
}
}
return {
...state,
serverConfig: action.payload
}
}
},
serverConfig: {
save: (state, action) => ({
...state,
serverConfig: action.payload,
serverError: null
})
},
tokens: {
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 = {
}
},
"httpRequest": function() {
return dispatch => {
dispatch(actions.discovery.start(null))
let url = '/.well-known/openid-configuration'
"httpRequest": function(url) {
return (dispatch, getState) => {
dispatch(actions.http.start(url))
var state = getState()
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": {}}
// config.headers.Authorization = 'Bearer ' + token.access_token
// config.headers["original-token"] = token.access_token
config.headers.Authorization = 'Bearer ' + state.token.access_token
config.method = "GET"
config.mode = "cors"
console.log("About to fetch", config)
let resp = null
return fetch(url, config).then((response) => {
resp = response
if (response.ok) {
return response.json()
}
throw Error(response.statusText)
})
.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) => {
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