Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#88] Implement Google client #90

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ jwt:
- name: v1
private: <private_key>
public: <public_key>
google:
oauth:
clientId: <client_id>
clientSecret: <client_secret>
redirectPath: /api/v1/google/sign-in/token
endpoints:
auth: https://accounts.google.com/o/oauth2/auth
token: https://oauth2.googleapis.com/token
oauth:
stateExpirationMinutes: 10

10 changes: 10 additions & 0 deletions api/config/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ jwt:
/uKYA36hQt+ou2Y7ITRR0raFePtwiwRfIT1cqeqG6+n+uMiT5kh70P6vQbglfCj9
1QIDAQAB
-----END PUBLIC KEY-----
google:
oauth:
clientId: test_client_id
clientSecret: test_client_secret
redirectPath: /api/v1/google/sign-in/token
endpoints:
auth: https://accounts.google.com/o/oauth2/auth
token: https://oauth2.googleapis.com/token
oauth:
stateExpirationMinutes: 10

20 changes: 19 additions & 1 deletion api/src/config/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import config from 'config';
import {
Config,
ConfigDatabase,
ConfigGoogle,
ConfigHttp,
ConfigJwt,
ConfigLogger,
ConfigOauth,
ConfigTimeout
} from '@src/config/types';
import { HttpConfig } from '@src/controller/http/types';
import { GoogleClientConfig } from '@src/infrastructure/google/types';
import { DatabaseConfig } from '@src/infrastructure/repositories/types';
import { JwtClientConfig } from '@src/jwt/types';
import { LoggerConfig } from '@src/logger/types';
Expand All @@ -21,7 +24,9 @@ export function loadConfig(): Config {
http: config.get<ConfigHttp>('http'),
logger: config.get<ConfigLogger>('logger'),
database: config.get<ConfigDatabase>('database'),
jwt: config.get<ConfigJwt>('jwt')
jwt: config.get<ConfigJwt>('jwt'),
google: config.get<ConfigGoogle>('google'),
oauth: config.get<ConfigOauth>('oauth')
};
} catch (e) {
throw new Error(`failed to load config error: ${e}`);
Expand Down Expand Up @@ -56,3 +61,16 @@ export function buildDatabaseConfig(config: Config): DatabaseConfig {
export function buildJwtClientConfig(config: Config): JwtClientConfig {
return config.jwt;
}

export function buildGoogleClientConfig(config: Config): GoogleClientConfig {
return {
oauth: {
clientId: config.google.oauth.clientId,
clientSecret: config.google.oauth.clientSecret,
redirectUri: `${config.http.host}:${config.http.port}${config.google.oauth.redirectPath}`,
authEndpoint: config.google.endpoints.auth,
tokenEndpoint: config.google.endpoints.token
}
};
}

22 changes: 22 additions & 0 deletions api/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface Config {
logger: ConfigLogger;
database: ConfigDatabase;
jwt: ConfigJwt;
google: ConfigGoogle;
oauth: ConfigOauth;
}

export interface ConfigTimeout {
Expand Down Expand Up @@ -36,3 +38,23 @@ export interface ConfigJwt {
keyPairs: RsaKeyPair[];
expirationHour: number;
}

export interface ConfigGoogle {
oauth: ConfigGoogleOauth;
endpoints: ConfigGoogleEndpoints;
}

export interface ConfigGoogleOauth {
clientId: string;
clientSecret: string;
redirectPath: string;
}

export interface ConfigGoogleEndpoints {
auth: string;
token: string;
}

export interface ConfigOauth {
stateExpirationMinutes: number;
}
3 changes: 3 additions & 0 deletions api/src/core/ports/google.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface GoogleHandler {
buildOidcRequest(protocol: string, state: string): string;
}
24 changes: 24 additions & 0 deletions api/src/infrastructure/google/google.client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { randomUUID } from 'crypto';
import { stringify } from 'querystring';

import { GoogleHandler } from '@src/core/ports/google.handler';
import { GoogleClientConfig } from '@src/infrastructure/google/types';

export class GoogleClient implements GoogleHandler {
constructor(private readonly config: GoogleClientConfig) {}

public buildOidcRequest(protocol: string, state: string): string {
const queryString = stringify({
client_id: this.config.oauth.clientId,
nonce: randomUUID(),
response_type: 'code',
redirect_uri: `${protocol}://${this.config.oauth.redirectUri}`,
scope: 'openid email',
state: state,
access_type: 'online',
prompt: 'consent select_account'
});

return `${this.config.oauth.authEndpoint}?${queryString}`;
}
}
11 changes: 11 additions & 0 deletions api/src/infrastructure/google/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface GoogleClientConfig {
oauth: GoogleOauthConfig;
}

interface GoogleOauthConfig {
clientId: string;
clientSecret: string;
redirectUri: string;
authEndpoint: string;
tokenEndpoint: string;
}
6 changes: 1 addition & 5 deletions api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ async function main() {
try {
jwtClient = new JwtClient(buildJwtClientConfig(config));
} catch (e) {
logger.log({
level: 'error',
message: 'Failed to initialize JWT client',
error: e
});
logger.error('Failed to initialize JWT client', { error: e });
process.exit(1);
}
const prismaClient = generatePrismaClient(buildDatabaseConfig(config));
Expand Down
29 changes: 29 additions & 0 deletions api/test/config/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import config from 'config';

import {
buildDatabaseConfig,
buildGoogleClientConfig,
buildHttpConfig,
buildJwtClientConfig,
buildLoggerConfig,
Expand Down Expand Up @@ -33,6 +34,20 @@ describe('Test config loader', () => {
'-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsNp22LRZ1VNL+0KI21Cd\n7XFIF5bdN4fiNjESGMcH8iylL0Pg924lYq/Jtt8wtBAVUn6W87hRqFN4jwrHfcaG\nd25aTf/MnR9asteSj8+d9K/s8UOLMQMRPjY0ud/NNL4Bn3LubjwLXjEkhV6bC9Le\nIZSAFiomjjqW2V4rFecffngdtOo8P77P19Rg5snQkLox1ABmVGE9+XWU2Hr1vZ+m\nh2WqwGmQr/j5Us6QZ8vvKR0oQdmzz9+P/1w/xwEYvZhZyn1hTWvBEufUQYWSb0ve\n/uKYA36hQt+ou2Y7ITRR0raFePtwiwRfIT1cqeqG6+n+uMiT5kh70P6vQbglfCj9\n1QIDAQAB\n-----END PUBLIC KEY-----\n'
}
]
},
google: {
oauth: {
clientId: 'test_client_id',
clientSecret: 'test_client_secret',
redirectPath: '/api/v1/google/sign-in/token'
},
endpoints: {
auth: 'https://accounts.google.com/o/oauth2/auth',
token: 'https://oauth2.googleapis.com/token'
}
},
oauth: {
stateExpirationMinutes: 10
}
});
});
Expand Down Expand Up @@ -101,3 +116,17 @@ describe('Test build jwt client config', () => {
});
});
});

describe('Test build google client config', () => {
it('should build valid google client config from a test.yaml', () => {
expect(buildGoogleClientConfig(loadConfig())).toStrictEqual({
oauth: {
clientId: 'test_client_id',
clientSecret: 'test_client_secret',
redirectUri: '127.0.0.1:0/api/v1/google/sign-in/token',
authEndpoint: 'https://accounts.google.com/o/oauth2/auth',
tokenEndpoint: 'https://oauth2.googleapis.com/token'
}
});
});
});
45 changes: 45 additions & 0 deletions api/test/infrastructure/google/google.client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { randomUUID } from 'crypto';
import { parse } from 'querystring';

import { GoogleClient } from '@src/infrastructure/google/google.client';
import { GoogleClientConfig } from '@src/infrastructure/google/types';

describe('Test google client', () => {
let client: GoogleClient;
let config: GoogleClientConfig;

beforeAll(() => {
config = {
oauth: {
clientId: 'test_client_id',
clientSecret: 'test_client_secret',
redirectUri: '127.0.0.1:0/api/v1/google/sign-in/token',
authEndpoint: 'https://accounts.google.com/o/oauth2/auth',
tokenEndpoint: 'https://oauth2.googleapis.com/token'
}
};
client = new GoogleClient(config);
});

it('should build valid OIDC request', () => {
const givenProtocol = 'http';
const givenState = randomUUID();
const expectedQueyObject = {
client_id: config.oauth.clientId,
response_type: 'code',
redirect_uri: `${givenProtocol}://${config.oauth.redirectUri}`,
scope: 'openid email',
state: givenState,
access_type: 'online',
prompt: 'consent select_account'
};

const result = client.buildOidcRequest(givenProtocol, givenState);
const [authEndpoint, queryString] = result.split('?');

expect(authEndpoint).toEqual(config.oauth.authEndpoint);
expect(parse(queryString)).toEqual(
expect.objectContaining(expectedQueyObject)
);
});
});