Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
3cae7b432d
@ -21,7 +21,6 @@ from app_be.views.rest_api import *
|
|||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
url(r'^test/', TestApiClass.test_api),
|
|
||||||
url(r'^api/login', LoginClass.login),
|
url(r'^api/login', LoginClass.login),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@ -3,48 +3,50 @@ import logging
|
|||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
|
|
||||||
from rest_framework.decorators import api_view
|
from rest_framework.decorators import api_view
|
||||||
from oauthlib import openid
|
|
||||||
from py_jwt_validator import PyJwtValidator, PyJwtException
|
from py_jwt_validator import PyJwtValidator, PyJwtException
|
||||||
import requests
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class TestApiClass:
|
def authorize(request):
|
||||||
@staticmethod
|
logger.debug('Validating request: {}'.format(request))
|
||||||
@api_view(['GET'])
|
|
||||||
def test_api(request):
|
if 'Authorization' not in request.headers:
|
||||||
logger.debug('Test api call: {}'.format(request))
|
print(f"Authorization header missing")
|
||||||
return JsonResponse({'Result': 'success'}, safe=False)
|
logger.error(f"Authorization header missing")
|
||||||
|
return None
|
||||||
|
|
||||||
|
bearer = request.headers['Authorization']
|
||||||
|
|
||||||
|
if len(bearer.split()) < 2:
|
||||||
|
return None
|
||||||
|
|
||||||
|
jwt = bearer.split()[1]
|
||||||
|
|
||||||
|
try:
|
||||||
|
validator = PyJwtValidator(jwt, auto_verify=False)
|
||||||
|
token = validator.verify(True)
|
||||||
|
if 'payload' in token:
|
||||||
|
payload = token['payload']
|
||||||
|
if 'sub' in payload:
|
||||||
|
return payload['sub']
|
||||||
|
except PyJwtException as e:
|
||||||
|
print(f"Exception caught. Error: {e}")
|
||||||
|
logger.error(f"Exception caught. Error: {e}")
|
||||||
|
return None
|
||||||
|
except UnicodeDecodeError as e2:
|
||||||
|
print(f"Exception caught. Error: {e2}")
|
||||||
|
logger.error(f"Exception caught. Error: {e2}")
|
||||||
|
return None
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
class LoginClass:
|
class LoginClass:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
def login(request: requests.Request):
|
def login(request):
|
||||||
logger.debug('Validating request: {}'.format(request))
|
user_sub = authorize(request)
|
||||||
|
if not user_sub:
|
||||||
if 'Authorization' not in request.headers:
|
|
||||||
print(f"Authorization header missing")
|
|
||||||
logger.error(f"Authorization header missing")
|
|
||||||
return JsonResponse({}, status=401)
|
return JsonResponse({}, status=401)
|
||||||
|
|
||||||
bearer = request.headers['Authorization']
|
return JsonResponse({'user': user_sub}, safe=False, status=200)
|
||||||
|
|
||||||
if len(bearer.split()) < 2:
|
|
||||||
return JsonResponse({}, status=401)
|
|
||||||
|
|
||||||
jwt = bearer.split()[1]
|
|
||||||
|
|
||||||
try:
|
|
||||||
PyJwtValidator(jwt)
|
|
||||||
except PyJwtException as e:
|
|
||||||
print(f"Exception caught. Error: {e}")
|
|
||||||
logger.error(f"Exception caught. Error: {e}")
|
|
||||||
return JsonResponse({}, status=401)
|
|
||||||
except UnicodeDecodeError as e2:
|
|
||||||
print(f"Exception caught. Error: {e2}")
|
|
||||||
logger.error(f"Exception caught. Error: {e2}")
|
|
||||||
return JsonResponse({}, status=401)
|
|
||||||
|
|
||||||
return JsonResponse({}, safe=False, status=200)
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
<app-navigation [activeLink]="'settings'"></app-navigation>
|
<app-navigation [activeLink]="'settings'"></app-navigation>
|
||||||
<div class="content">
|
<div class="content">
|
||||||
<div class="text-center">
|
<div class="text-center">
|
||||||
<div class="container" *ngFor="let number of [1, 2, 3]">
|
<div class="container" *ngFor="let number of [1]">
|
||||||
<div class="row feed-list-row">
|
<div class="row feed-list-row">
|
||||||
<div class="col-2 text-center padding-0 margin-auto">
|
<div class="col-2 text-center padding-0 margin-auto">
|
||||||
<img class="feed-icon" src="assets/logo.svg" alt="Feed-Icon">
|
<img class="feed-icon" src="assets/logo.svg" alt="Feed-Icon">
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
import { Component, OnInit } from '@angular/core';
|
import { Component, OnInit } from '@angular/core';
|
||||||
|
import {AuthService} from '../../services/auth.service';
|
||||||
|
import {HttpClient, HttpHeaders} from '@angular/common/http';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-tweets',
|
selector: 'app-tweets',
|
||||||
@ -7,13 +9,24 @@ import { Component, OnInit } from '@angular/core';
|
|||||||
})
|
})
|
||||||
export class TweetsComponent implements OnInit {
|
export class TweetsComponent implements OnInit {
|
||||||
|
|
||||||
constructor() { }
|
constructor(private http: HttpClient,
|
||||||
|
private authService: AuthService) { }
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
}
|
}
|
||||||
|
|
||||||
loadMore() {
|
loadMore() {
|
||||||
console.log('TODO: Implement');
|
console.log('TODO: Implement');
|
||||||
}
|
const headerDict = {
|
||||||
|
'Authorization': 'Bearer ' + this.authService.getToken(),
|
||||||
|
};
|
||||||
|
return this.http.get('http://localhost:8000/api/login',
|
||||||
|
{
|
||||||
|
headers: new HttpHeaders(headerDict),
|
||||||
|
observe: 'response',
|
||||||
|
})
|
||||||
|
.subscribe(data => { console.log(data); alert('Returned with code: ' + data['status']); });
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user