Skip to content

Commit

Permalink
feat: applies the keycloak access token to each request
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlkoch committed Jun 21, 2022
1 parent 0930303 commit 1fba0a7
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 25 deletions.
11 changes: 9 additions & 2 deletions src/service/AppInfoService/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import { getCsrfTokenHeader } from '../../security/getCsrfTokenHeader';
import Keycloak from 'keycloak-js';

import { getBearerTokenHeader } from '../../security/getBearerTokenHeader';

import { AppInfo } from '../../model/AppInfo';

export interface AppInfoServiceOpts {
basePath: string;
keycloak?: Keycloak;
};

export class AppInfoService {

private basePath: string;

private keycloak?: Keycloak;

constructor(opts: AppInfoServiceOpts = {
basePath: '/info'
}) {
this.basePath = opts.basePath;
this.keycloak = opts.keycloak;
}

async getAppInfo(fetchOpts?: RequestInit): Promise<AppInfo> {
try {
const response = await fetch(`${this.basePath}/app`, {
method: 'GET',
headers: {
...getCsrfTokenHeader()
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});
Expand Down
11 changes: 10 additions & 1 deletion src/service/AuthService/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import Keycloak from 'keycloak-js';

import { getBearerTokenHeader } from '../../security/getBearerTokenHeader';
import { getCsrfTokenHeader } from '../../security/getCsrfTokenHeader';

export interface AuthServiceOpts {
basePath: string;
keycloak?: Keycloak;
};

export class AuthService {

private basePath: string;

private keycloak?: Keycloak;

constructor(opts: AuthServiceOpts = {
basePath: '/sso'
}) {
this.basePath = opts.basePath;
this.keycloak = opts.keycloak;
}

async logout(requestOpts?: RequestInit): Promise<void> {
Expand All @@ -19,7 +27,8 @@ export class AuthService {
method: 'POST',
credentials: 'same-origin',
headers: {
...getCsrfTokenHeader()
...getCsrfTokenHeader(),
...getBearerTokenHeader(this.keycloak)
},
...requestOpts
});
Expand Down
11 changes: 10 additions & 1 deletion src/service/CacheService/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import Keycloak from 'keycloak-js';

import { getBearerTokenHeader } from '../../security/getBearerTokenHeader';
import { getCsrfTokenHeader } from '../../security/getCsrfTokenHeader';

export interface CacheServiceOpts {
basePath: string;
keycloak?: Keycloak;
};

export class CacheService {

private basePath: string;

private keycloak?: Keycloak;

constructor(opts: CacheServiceOpts = {
basePath: '/cache'
}) {
this.basePath = opts.basePath;
this.keycloak = opts.keycloak;
}

async evictCache(fetchOpts?: RequestInit): Promise<void> {
try {
const response = await fetch(`${this.basePath}/evict`, {
method: 'POST',
headers: {
...getCsrfTokenHeader()
...getCsrfTokenHeader(),
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});
Expand Down
16 changes: 12 additions & 4 deletions src/service/GenericFileService/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import Keycloak from 'keycloak-js';

import { getBearerTokenHeader } from '../../security/getBearerTokenHeader';
import { getCsrfTokenHeader } from '../../security/getCsrfTokenHeader';

import SHOGunFile from '../../model/File';

export interface GenericFileServiceOpts {
basePath: string;
keycloak?: Keycloak;
};

export abstract class GenericFileService<T extends SHOGunFile> {

basePath: string;

keycloak?: Keycloak;

constructor(opts: GenericFileServiceOpts) {
this.basePath = opts.basePath;
this.keycloak = opts.keycloak;
}

async findAll(fetchOpts?: RequestInit): Promise<T[]> {
try {
const response = await fetch(this.basePath, {
method: 'GET',
headers: {
...getCsrfTokenHeader()
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});
Expand All @@ -41,7 +48,7 @@ export abstract class GenericFileService<T extends SHOGunFile> {
const response = await fetch(`${this.basePath}/${fileUuid}`, {
method: 'GET',
headers: {
...getCsrfTokenHeader()
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});
Expand All @@ -67,7 +74,8 @@ export abstract class GenericFileService<T extends SHOGunFile> {
const response = await fetch(`${this.basePath}/${fileSystem ? 'uploadToFileSystem' : 'upload'}`, {
method: 'POST',
headers: {
...getCsrfTokenHeader()
...getCsrfTokenHeader(),
...getBearerTokenHeader(this.keycloak)
},
body: formData,
...fetchOpts
Expand All @@ -90,7 +98,7 @@ export abstract class GenericFileService<T extends SHOGunFile> {
const response = await fetch(`${this.basePath}/${fileUuid}`, {
method: 'DELETE',
headers: {
...getCsrfTokenHeader()
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});
Expand Down
22 changes: 16 additions & 6 deletions src/service/GenericService/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import Keycloak from 'keycloak-js';

import { getBearerTokenHeader } from '../../security/getBearerTokenHeader';
import { getCsrfTokenHeader } from '../../security/getCsrfTokenHeader';

import BaseEntity from '../../model/BaseEntity';

export interface GenericServiceOpts {
basePath: string;
keycloak?: Keycloak;
};

export abstract class GenericService<T extends BaseEntity> {

basePath: string;

keycloak?: Keycloak;

constructor(opts: GenericServiceOpts) {
this.basePath = opts.basePath;
this.keycloak = opts.keycloak;
}

async findAll(fetchOpts?: RequestInit): Promise<T[]> {
try {
const response = await fetch(this.basePath, {
method: 'GET',
headers: {
...getCsrfTokenHeader()
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});
Expand All @@ -41,7 +48,7 @@ export abstract class GenericService<T extends BaseEntity> {
const response = await fetch(`${this.basePath}/${id}`, {
method: 'GET',
headers: {
...getCsrfTokenHeader()
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});
Expand All @@ -64,7 +71,8 @@ export abstract class GenericService<T extends BaseEntity> {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...getCsrfTokenHeader()
...getCsrfTokenHeader(),
...getBearerTokenHeader(this.keycloak)
},
body: JSON.stringify(t),
...fetchOpts
Expand All @@ -88,7 +96,8 @@ export abstract class GenericService<T extends BaseEntity> {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
...getCsrfTokenHeader()
...getCsrfTokenHeader(),
...getBearerTokenHeader(this.keycloak)
},
body: JSON.stringify(t),
...fetchOpts
Expand All @@ -112,7 +121,8 @@ export abstract class GenericService<T extends BaseEntity> {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
...getCsrfTokenHeader()
...getCsrfTokenHeader(),
...getBearerTokenHeader(this.keycloak)
},
body: JSON.stringify(t),
...fetchOpts
Expand All @@ -135,7 +145,7 @@ export abstract class GenericService<T extends BaseEntity> {
const response = await fetch(`${this.basePath}/${id}`, {
method: 'DELETE',
headers: {
...getCsrfTokenHeader()
...getBearerTokenHeader(this.keycloak)
},
...fetchOpts
});
Expand Down
10 changes: 9 additions & 1 deletion src/service/GraphQLService/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Keycloak from 'keycloak-js';

import { getBearerTokenHeader } from '../../security/getBearerTokenHeader';
import { getCsrfTokenHeader } from '../../security/getCsrfTokenHeader';

export interface GraphQLQueryObject {
Expand All @@ -14,16 +17,20 @@ export interface GraphQLResponse<T> {

export interface GraphQLServiceOpts {
basePath: string;
keycloak?: Keycloak;
};

export class GraphQLService {

private basePath: string;

private keycloak?: Keycloak;

constructor(opts: GraphQLServiceOpts = {
basePath: '/graphql'
}) {
this.basePath = opts.basePath;
this.keycloak = opts.keycloak;
}

async sendQuery<T>(query: GraphQLQueryObject, fetchOpts?: RequestInit): Promise<GraphQLResponse<T>> {
Expand All @@ -32,7 +39,8 @@ export class GraphQLService {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...getCsrfTokenHeader()
...getCsrfTokenHeader(),
...getBearerTokenHeader(this.keycloak)
},
body: JSON.stringify(query),
...fetchOpts
Expand Down
Loading

0 comments on commit 1fba0a7

Please sign in to comment.