Skip to content
Snippets Groups Projects
Commit 9cd09f98 authored by Nils Brinckmann's avatar Nils Brinckmann
Browse files

Strategy to send the id token for every sms request

parent 4a8572c1
No related branches found
No related tags found
1 merge request!78Resolve "Add JWT Token from OIDC for post, patch & delete requests"
......@@ -73,7 +73,9 @@ export class Api {
constructor (
getIdToken: () => string | null,
smsBaseUrl: string | undefined = SMS_BASE_URL, cvBaseUrl: string | undefined = CV_BASE_URL) {
smsBaseUrl: string | undefined = SMS_BASE_URL,
cvBaseUrl: string | undefined = CV_BASE_URL
) {
// here we can set settings for all the sms api calls
const smsConfig: AxiosRequestConfig = {
// for the SMS Backend we need the explicit vnd.api+json
......@@ -81,18 +83,19 @@ export class Api {
'Content-Type': 'application/vnd.api+json'
}
}
// For the sms we also want to send the id token, if we currently
// have one in the store.
this._contactApi = new ContactApi(
this.createAxios(smsBaseUrl, '/contacts', smsConfig), getIdToken
this.createAxios(smsBaseUrl, '/contacts', smsConfig, getIdToken)
)
this._platformApi = new PlatformApi(
this.createAxios(smsBaseUrl, '/platforms', smsConfig)
this.createAxios(smsBaseUrl, '/platforms', smsConfig, getIdToken)
)
this._deviceApi = new DeviceApi(
this.createAxios(smsBaseUrl, '/devices', smsConfig)
this.createAxios(smsBaseUrl, '/devices', smsConfig, getIdToken)
)
this._configurationApi = new ConfigurationApi(
this.createAxios(smsBaseUrl, '/configurations', smsConfig)
this.createAxios(smsBaseUrl, '/configurations', smsConfig, getIdToken)
)
this._configurationStatesApi = new ConfigurationStatusApi()
......@@ -136,12 +139,28 @@ export class Api {
this._projectApi = new ProjectApi()
}
private createAxios (baseUrl: string | undefined, path: string, baseConfig: AxiosRequestConfig): AxiosInstance {
private createAxios (baseUrl: string | undefined, path: string, baseConfig: AxiosRequestConfig, getIdToken?: () => (string | null)): AxiosInstance {
const config = {
...baseConfig,
baseURL: baseUrl + path
}
return axios.create(config)
const instance = axios.create(config)
// If we have a function to query our id tokens on the time of the request
// we want to use it here.
if (getIdToken) {
instance.interceptors.request.use((config) => {
const idToken = getIdToken()
// But it can be that we are not logged in, so that our idToken is null.
// So in this case, we don't send the id token with the request.
if (idToken) {
// But once we have it, we want to send it with.
config.headers.Authorization = 'Bearer ' + idToken
}
return config
})
}
return instance
}
get devices (): DeviceApi {
......
......@@ -37,12 +37,10 @@ import { IPaginationLoader, FilteredPaginationedLoader } from '@/utils/Paginated
export class ContactApi {
private axiosApi: AxiosInstance
private getIdToken: () => string | null
private serializer: ContactSerializer
constructor (axiosInstance: AxiosInstance, getIdToken: () => string | null) {
constructor (axiosInstance: AxiosInstance) {
this.axiosApi = axiosInstance
this.getIdToken = getIdToken
this.serializer = new ContactSerializer()
}
......@@ -58,19 +56,7 @@ export class ContactApi {
}
deleteById (id: string): Promise<void> {
let accessHeaders = {}
const token = this.getIdToken()
if (token) {
accessHeaders = {
Authorization: 'Bearer ' + token
}
}
return this.axiosApi.delete<string, void>(id,
{
headers: {
...accessHeaders
}
})
return this.axiosApi.delete<string, void>(id)
}
save (contact: Contact) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment