Skip to content

Commit

Permalink
a little auth for angelos endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Unischneider committed Nov 2, 2024
1 parent 7815213 commit 1b43819
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 7 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ jobs:
source: "./docker-compose.yml"
target: /home/${{ vars.VM_USERNAME }}/${{ github.repository }}

- name: Create Environment File
env:
ANGULAR_APP_API_KEY: ${{ secrets.ANGELOS_APP_API_KEY }}
run: |
echo "export const environment = {
production: true,
angelosUrl: '/api/v1/question/chat',
angelosToken: '/api/token',
angelosAppApiKey: '${ANGELOS_APP_API_KEY}'
};" > src/environments/environment.prod.ts
- name: SSH to VM and Execute Docker-Compose Up
uses: appleboy/[email protected]
with:
Expand Down
1 change: 1 addition & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ server {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Internal-Access "true";
}

error_page 500 502 503 504 /50x.html;
Expand Down
16 changes: 16 additions & 0 deletions src/app/services/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { AuthService } from './auth.service';

describe('AuthService', () => {
let service: AuthService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(AuthService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
31 changes: 31 additions & 0 deletions src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, tap } from 'rxjs';
import { environment } from '../../environments/environment';

export interface AuthResponse {
access_token: string;
token_type: string;
}

@Injectable({
providedIn: 'root'
})
export class AuthService {

constructor(private http: HttpClient) { }

login(): Observable<AuthResponse> {
const headers = new HttpHeaders().set('x-api-key', environment.angelosAppApiKey);
return this.http.post<AuthResponse>(environment.angelosToken, { headers }).pipe(
tap((response: AuthResponse) => {
sessionStorage.setItem('access_token', response.access_token);
})
);
}

// Method to retrieve the stored token
public getToken(): string | null {
return sessionStorage.getItem('access_token');
}
}
28 changes: 24 additions & 4 deletions src/app/services/chatbot.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ChatMessage } from '../chat/chat.component';
import { Observable } from 'rxjs';
import { Observable, switchMap } from 'rxjs';
import { environment } from '../../environments/environment';
import { AuthService } from './auth.service';

@Injectable({
providedIn: 'root'
})
export class ChatbotService {

constructor(private http: HttpClient) { }
constructor(private http: HttpClient, private authService: AuthService) { }

sendBotRequest(token: string | null, chatHistory: ChatMessage[], study_program: string): Observable<any> {
const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`);
return this.http.post(environment.angelosUrl,
{ messages: chatHistory, study_program: study_program },
{ headers }
);
}

getBotResponse(chatHistory: ChatMessage[], study_program: string): Observable<any> {
return this.http.post(environment.angelosUrl, { messages: chatHistory, study_program: study_program });
const token = this.authService.getToken();
if (token) {
return this.sendBotRequest(token, chatHistory, study_program);
} else {
// Login if no token is stored, then proceed with the bot request
return this.authService.login().pipe(
switchMap(() => {
const newToken = this.authService.getToken();
return this.sendBotRequest(newToken, chatHistory, study_program);
})
);
}
}
}
6 changes: 4 additions & 2 deletions src/environments/environment.development.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const environment = {
production: false,
angelosUrl: 'http://localhost:8000/api/v1/question/chat'
};
angelosUrl: 'http://localhost:8000/api/v1/question/chat',
angelosToken: 'http://localhost:8000/api/token',
angelosAppApiKey: 'SOME_SECRET_KEY',
};
4 changes: 3 additions & 1 deletion src/environments/environment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const environment = {
production: true,
angelosUrl: '/api/v1/question/chat'
angelosUrl: '/api/v1/question/chat',
angelosToken: '/api/token',
angelosAppApiKey: 'SOME_SECRET_KEY',
};

0 comments on commit 1b43819

Please sign in to comment.