Sixtens hemsida Uppgifter Blogg Om

VED House bokningssystem

Gå till sida

Källkod

api/

database.php

api.php

ved/

monthBookingData.php

openingHoursException.php

bookingData.php

dayTimeframes.php

bookings.php

admin/

revokeToken.php

authorization.php

login.php

modules/

vedDatabase.php

utility.php

gyar/

log.php

auth/

revokeToken.php

authorization.php

login.php

station/

settings.php

log.php

modules/

gyarDatabase.php

databaseConnection.php

database.php

utility.php

credentials.json

weather/

latest.php

old.php

admin/

index.html

changeTimetable.html

login.html

js/

login.js

modules.js

admin.js

header.js

changeTimetable.js

cron/

vedClearExpiredTokens.php

api/gyar/auth/authorization.php

1 lines
<?php
# authorization.php
# Funktion som API endpoints använder för att kontrollera authorization token.

enum AuthLevel
{
    case 
STATION;
    case 
ADMIN;
}

# Hämtar vilken nivå av tillåtelse som förfrågas.
function getAuthLevel()
{
    
$headers getallheaders();
    if (!isset(
$headers["Authorization"]))
        return [
nullnull];
    
$authData explode(" "$headers["Authorization"]);
    if (
sizeof($authData) != 2)
        return [
nullnull];
    
$authType $authData[0];
    
$authToken $authData[1];

    require 
"gyar/modules/database.php";
    
$hashed hash("sha256"$authToken);
    if (
$authType == "Bearer") {
        
$token $db->query("SELECT tokenId, userId FROM authToken WHERE tokenHash=:tokenHash AND expiresAt>CURRENT_TIMESTAMP AND (revokedAt IS NULL OR revokedAt>CURRENT_TIMESTAMP)", array("tokenHash" => $hashed));
        if (!
$token)
            return [
nullnull];
        return [
AuthLevel::ADMIN$token["userId"]];
    } elseif (
$authType == "Station") {
        
$station $db->query("SELECT stationId FROM weatherStation WHERE apiKeyHash=:apiKeyHash", array("apiKeyHash" => $hashed));
        if (!
$station)
            return [
nullnull];
        return [
AuthLevel::STATION$station["stationId"]];
    }
    return [
nullnull];
}

function 
acceptAuthorizationLevels($allowedLevels)
{
    if (!
is_array($allowedLevels))
        
$allowedLevels = [$allowedLevels];
    [
$requestedLevel$data] = getAuthLevel();
    if (!
$requestedLevel)
        throw new 
APIError(401"Invalid authorization.");

    foreach (
$allowedLevels as $level) {
        if (
$level == $requestedLevel)
            return 
$data;
    }
    throw new 
APIError(403"Missing access to the requested resource.");
}
?>