Commit 75e526db authored by Revant Nandgaonkar's avatar Revant Nandgaonkar

feat(communication-server): Added OAuth2-Provider

Merge branch 'OAuth2-Provider' into 'develop'

See merge request castlecraft/building-blocks!292
parents cccac895 23a63c30
......@@ -36,6 +36,9 @@ export class OAuth2Provider extends BaseEntity {
@Column()
revocationURL: string;
@Column()
scope: string[];
constructor() {
super();
if (!this.uuid) this.uuid = uuidv4();
......
import { IsString, IsUrl, IsNotEmpty, IsOptional } from 'class-validator';
import {
IsString,
IsUrl,
IsNotEmpty,
IsOptional,
IsArray,
} from 'class-validator';
export class OAuth2ProviderDto {
@IsString()
......@@ -28,4 +34,8 @@ export class OAuth2ProviderDto {
@IsUrl()
revocationURL: string;
@IsArray()
@IsNotEmpty({ each: true })
scope: string[];
}
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { SocialLoginComponent } from './social-login.component';
import { MaterialModule } from '../../shared-imports/material/material.module';
import { SocialLoginService } from './social-login.service';
......
......@@ -31,7 +31,7 @@
<input
[type]="hideClientSecret ? 'password' : 'text'"
matInput placeholder="Client Secret" formControlName="clientSecret"
mat-raised-button>
mat-raised-button required>
<mat-icon
matSuffix
(click)="hideClientSecret = !hideClientSecret">
......@@ -84,8 +84,23 @@
mat-raised-button
required>
</mat-form-field>
<div
*ngFor="let scp of providerForm.get('scope')['controls']; let i = index;"
formArrayName="scope">
<div [formGroupName]="i">
<mat-form-field>
<input matInput formControlName="scope" placeholder="Scope {{i+1}}">
<button mat-icon-button color="accent" matSuffix (click)="removeScope(i)">
<mat-icon>close</mat-icon>
</button>
</mat-form-field>
</div>
</div>
<button mat-icon-button color="accent" (click)="addScope()">
Add Scope <mat-icon>add</mat-icon>
</button>
</mat-card-content>
<mat-card-actions>
<mat-card-actions fxLayout="row" fxLayoutGap="1em">
<button
(click)="updateProvider()"
mat-flat-button color="primary"
......@@ -97,6 +112,11 @@
color="primary"
[disabled]="!providerForm.valid"
*ngIf="!name">Create</button>
<!-- <button
(click)="connectAccount()"
mat-flat-button color="accent"
[disabled]="!providerForm.valid"
*ngIf="name">Connect</button> -->
</mat-card-actions>
</mat-card>
</form>
......
......@@ -14,12 +14,13 @@ describe('OAuth2ProviderComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
MaterialModule,
FormsModule,
ReactiveFormsModule,
RouterTestingModule,
BrowserAnimationsModule,
],
declarations: [OAuth2ProviderComponent],
providers: [
{
provide: OAuth2ProviderService,
......@@ -29,7 +30,6 @@ describe('OAuth2ProviderComponent', () => {
},
},
],
declarations: [OAuth2ProviderComponent],
}).compileComponents();
}));
......
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
import { FormGroup, FormControl, FormArray } from '@angular/forms';
import { OAuth2ProviderService } from './oauth2-provider.service';
import { MatSnackBar } from '@angular/material';
import { Router, ActivatedRoute } from '@angular/router';
......@@ -23,6 +23,9 @@ export class OAuth2ProviderComponent implements OnInit {
uuid: string;
hideClientSecret: boolean = true;
name: string;
scope: string[];
scopesForm = new FormArray([]);
providerForm = new FormGroup({
name: new FormControl(),
authServerURL: new FormControl(),
......@@ -34,6 +37,7 @@ export class OAuth2ProviderComponent implements OnInit {
introspectionURL: new FormControl(),
authorizationURL: new FormControl(),
revocationURL: new FormControl(),
scope: this.scopesForm,
});
constructor(
......@@ -46,34 +50,10 @@ export class OAuth2ProviderComponent implements OnInit {
}
ngOnInit() {
if (this.uuid !== NEW_ID) {
this.service.getProvider(this.uuid).subscribe({
next: response => {
this.name = response.name;
this.providerForm.controls.name.setValue(response.name);
this.providerForm.controls.authServerURL.setValue(
response.authServerURL,
);
this.providerForm.controls.clientId.setValue(response.clientId);
this.providerForm.controls.clientSecret.setValue(
response.clientSecret,
);
this.providerForm.controls.redirectURI.setValue(
this.service.generateRedirectURL(this.uuid),
);
this.providerForm.controls.profileURL.setValue(response.profileURL);
this.providerForm.controls.tokenURL.setValue(response.tokenURL);
this.providerForm.controls.introspectionURL.setValue(
response.introspectionURL,
);
this.providerForm.controls.authorizationURL.setValue(
response.authorizationURL,
);
this.providerForm.controls.revocationURL.setValue(
response.revocationURL,
);
},
});
if (this.uuid && this.uuid !== NEW_ID) {
this.subscribeGetSocialLogin(this.uuid);
} else if (this.uuid === NEW_ID) {
this.uuid = undefined;
}
}
......@@ -89,6 +69,7 @@ export class OAuth2ProviderComponent implements OnInit {
this.providerForm.controls.introspectionURL.value,
this.providerForm.controls.authorizationURL.value,
this.providerForm.controls.revocationURL.value,
this.getScopes(),
)
.subscribe({
next: response => {
......@@ -114,6 +95,7 @@ export class OAuth2ProviderComponent implements OnInit {
this.providerForm.controls.introspectionURL.value,
this.providerForm.controls.authorizationURL.value,
this.providerForm.controls.revocationURL.value,
this.getScopes(),
)
.subscribe({
next: response => {
......@@ -125,4 +107,55 @@ export class OAuth2ProviderComponent implements OnInit {
},
});
}
removeScope(formGroupID: number) {
this.scopesForm.removeAt(formGroupID);
}
addScope(scope?: string) {
this.scopesForm.push(new FormGroup({ scope: new FormControl(scope) }));
}
getScopes(): string[] {
const scopesFormGroup = this.providerForm.get('scope') as FormArray;
const scopes: string[] = [];
for (const control of scopesFormGroup.controls) {
scopes.push(control.value.scope);
}
return scopes;
}
subscribeGetSocialLogin(uuid: string) {
this.service.getProvider(this.uuid).subscribe({
next: response => {
if (response) {
this.populateForm(response);
}
},
});
}
populateForm(response) {
this.name = response.name;
this.providerForm.controls.name.setValue(response.name);
this.providerForm.controls.authServerURL.setValue(response.authServerURL);
this.providerForm.controls.clientId.setValue(response.clientId);
this.providerForm.controls.clientSecret.setValue(response.clientSecret);
this.providerForm.controls.redirectURI.setValue(
this.service.generateRedirectURL(this.uuid),
);
this.providerForm.controls.profileURL.setValue(response.profileURL);
this.providerForm.controls.tokenURL.setValue(response.tokenURL);
this.providerForm.controls.introspectionURL.setValue(
response.introspectionURL,
);
this.providerForm.controls.authorizationURL.setValue(
response.authorizationURL,
);
this.providerForm.controls.revocationURL.setValue(response.revocationURL);
this.scope = response.scope;
this.scope.forEach(scope => {
this.addScope(scope);
});
}
}
......@@ -30,6 +30,7 @@ export class OAuth2ProviderService {
introspectionURL: string,
authorizationURL: string,
revocationURL: string,
scope: string[],
) {
const url = `${this.storageService.getServiceURL(
COMMUNICATION_SERVER,
......@@ -44,6 +45,7 @@ export class OAuth2ProviderService {
introspectionURL,
authorizationURL,
revocationURL,
scope,
};
return this.http.post(url, payload, {
......@@ -62,6 +64,7 @@ export class OAuth2ProviderService {
introspectionURL: string,
authorizationURL: string,
revocationURL: string,
scope: string[],
) {
const url = `${this.storageService.getServiceURL(
COMMUNICATION_SERVER,
......@@ -76,6 +79,7 @@ export class OAuth2ProviderService {
introspectionURL,
authorizationURL,
revocationURL,
scope,
};
return this.http.post(url, payload, {
......
......@@ -45,7 +45,7 @@
<mat-nav-list>
<a mat-list-item routerLink="/email/list">Email</a>
<a mat-list-item routerLink="/storage/list">Storage</a>
<!-- <a mat-list-item routerLink="/oauth2_provider/list">OAuth2 Provider</a> -->
<a mat-list-item routerLink="/oauth2_provider/list">OAuth2 Provider</a>
<a mat-list-item routerLink="/communication_settings">Settings</a>
</mat-nav-list>
</mat-expansion-panel>
......
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