Skip to content

Commit

Permalink
Merge pull request #52 from amosproj/44-create-modules-and-organize-code
Browse files Browse the repository at this point in the history
44 create modules and organize code #44
  • Loading branch information
FlinkbaumFAU committed Jun 7, 2022
2 parents 52bbacd + 8a3a5e7 commit f6b45e2
Show file tree
Hide file tree
Showing 27 changed files with 530 additions and 221 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ CLIENT_CREDENTIALS_STRING=client_credentials
CLIENT_ID=
CLIENT_SECRET=
AUDIENCE=
ISSUER_STRING=
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
"binary": "js2bin --cache --build --platform=windows --platform=linux --platform=darwin --app=bundle/index.js --name=oidc"
},
"dependencies": {
"@nestjs/common": "^8.0.0",
"@nestjs/common": "^8.4.6",
"@nestjs/config": "^2.0.1",
"@nestjs/core": "^8.0.0",
"@nestjs/core": "^8.4.6",
"@nestjs/platform-express": "^8.0.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
Expand All @@ -46,19 +46,19 @@
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"@vercel/ncc": "^0.34.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "28.0.3",
"js2bin": "^1.0.7",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "28.0.1",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "4.0.0",
"@vercel/ncc": "^0.34.0",
"js2bin": "^1.0.7",
"typescript": "^4.3.5"
},
"jest": {
Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0; url=http://localhost:8081/api/issuer" />
<meta http-equiv="refresh" content="0; url=http://localhost:8081/api/discovery/issuer" />
</head>
</html>
141 changes: 0 additions & 141 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,145 +23,4 @@ export class AppController {
return;
}

async get_issuer(issuer_s) {
const issuer = await Issuer.discover(issuer_s);
return issuer;
}

@Get('issuer')
@Render('index')
async discover_issuer(
@Query('issuer_url') issuer_url_s: string,
@Query('authorization_endpoint') authorization_endpoint_s: string,
@Query('claim_types_supported') claim_types_supported_s: string,
@Query('claims_parameter_supported') claims_parameter_supported_s: string,
@Query('claims_supported') claims_supported_s: string,
@Query('code_challenge_methods_supported')
code_challenge_methods_supported_s: string,
@Query('device_authorization_endpoint')
device_authorization_endpoint_s: string,
@Query('grant_types_supported') grant_types_supported_s: string,
@Query('id_token_signing_alg_values_supported')
id_token_signing_alg_values_supported_s: string,
@Query('issuer') issuer_s: string,
@Query('jwks_uri') jwks_uri_s: string,
@Query('request_parameter_supported') request_parameter_supported_s: string,
@Query('request_uri_parameter_supported')
request_uri_parameter_supported_s: string,
@Query('require_request_uri_registration')
require_request_uri_registration_s: string,
@Query('response_modes_supported') response_modes_supported_s: string,
@Query('response_types_supported') response_types_supported_s: string,
@Query('revocation_endpoint') revocation_endpoint_s: string,
@Query('revocation_endpoint_auth_methods_supported')
revocation_endpoint_auth_methods_supported_s: string,
@Query('scopes_supported') scopes_supported_s: string,
@Query('subject_types_supported') subject_types_supported_s: string,
@Query('token_endpoint') token_endpoint_s: string,
@Query('token_endpoint_auth_methods_supported')
token_endpoint_auth_methods_supported_s: string,
@Query('userinfo_endpoint') userinfo_endpoint_s: string,
) {
const checkboxes = {
authorization_endpoint: authorization_endpoint_s,
claim_types_supported: claim_types_supported_s,
claims_parameter_supported: claims_parameter_supported_s,
claims_supported: claims_supported_s,
code_challenge_methods_supported: code_challenge_methods_supported_s,
device_authorization_endpoint: device_authorization_endpoint_s,
grant_types_supported: grant_types_supported_s,
id_token_signing_alg_values_supported:
id_token_signing_alg_values_supported_s,
issuer: issuer_s,
jwks_uri: jwks_uri_s,
request_parameter_supported: request_parameter_supported_s,
request_uri_parameter_supported: request_uri_parameter_supported_s,
require_request_uri_registration: require_request_uri_registration_s,
response_modes_supported: response_modes_supported_s,
response_types_supported: response_types_supported_s,
revocation_endpoint: revocation_endpoint_s,
revocation_endpoint_auth_methods_supported:
revocation_endpoint_auth_methods_supported_s,
scopes_supported: scopes_supported_s,
subject_types_supported: subject_types_supported_s,
token_endpoint: token_endpoint_s,
token_endpoint_auth_methods_supported:
token_endpoint_auth_methods_supported_s,
userinfo_endpoint: userinfo_endpoint_s,
};
let keys = [];
for (const key in checkboxes) {
if (checkboxes[key] === '1') {
keys.push(key);
}
}
if (issuer_url_s === undefined) {
return {
result: {
success: 2,
info: null,
previously_checked: null,
},
first_query: 1,
};
}
return {
result: await this.get_issuer(issuer_url_s)
.then((issuer) => {
//console.log(issuer);
return {
success: 1,
info: JSON.stringify(issuer, keys, 2),
previously_checked: checkboxes,
};
})
.catch((err) => {
return {
success: 0,
info: err,
previously_checked: null,
};
}),
};
}

@Get('token')
async requestToken(
@Query('issuer') issuer_s: string,
@Res() res: Response,
): Promise<any> {
const issuer = await this.get_issuer(issuer_s).catch(() => {
throw new HttpException(
{
status: HttpStatus.BAD_REQUEST,
error: 'invalid issuer',
},
HttpStatus.BAD_REQUEST,
);
});
const result = await this.appService.requestToken(issuer);
res.json(result.data).send();
}

@Post('/token')
async requestTokenWithClientInformation(
@Query('issuer') issuer_s: string,
@Body() grantBody: GrantBody,
@Res() res: Response,
): Promise<any> {
const issuer = await this.get_issuer(issuer_s).catch(() => {
throw new HttpException(
{
status: HttpStatus.BAD_REQUEST,
error: 'invalid issuer',
},
HttpStatus.BAD_REQUEST,
);
});
const result = await this.appService.getToken(
String(issuer.token_endpoint),
grantBody,
);
res.json(result.data).send();
}
}
10 changes: 9 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ import { UserModule } from './user/user.module';
import { AppController } from './app.controller';
import { ConfigModule } from '@nestjs/config';
import { TokenModule } from './token/token.module';
import { DiscoveryModule } from './discovery/discovery.module';
import { FlowsModule } from './flows/flows.module';

@Module({
imports: [ConfigModule.forRoot(), UserModule, TokenModule],
imports: [
ConfigModule.forRoot(),
UserModule,
DiscoveryModule,
TokenModule,
FlowsModule,
],
providers: [AppService],
controllers: [AppController],
})
Expand Down
29 changes: 0 additions & 29 deletions src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,4 @@ export class AppService {
getHello(): string {
return 'Hello World!';
}

async getToken(token_endpoint: string, grantBody: GrantBody): Promise<any> {
return await axios
.post(token_endpoint, qs.stringify(grantBody), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
})
.catch(() => {
throw new HttpException(
{
status: HttpStatus.UNAUTHORIZED,
error: 'Access denied',
},
HttpStatus.UNAUTHORIZED,
);
});
}

async requestToken(issuer: Issuer): Promise<any> {
const grantBody: GrantBody = {
grant_type: process.env.CLIENT_CREDENTIALS_STRING,
client_id: process.env.CLIENT_ID,
client_secret: process.env.CLIENT_SECRET,
audience: process.env.AUDIENCE,
};

return await this.getToken(String(issuer.token_endpoint), grantBody);
}
}
Loading

0 comments on commit f6b45e2

Please sign in to comment.