Files
Testing/avsecure.php

117 lines
3.9 KiB
PHP
Raw Permalink Normal View History

2026-11-17 16:15:01 -05:00
<?php
class AVSecure {
// CUSTOM_LOGIN_PAGE allows to customize default login page.
// If set content of the customized page will be used as a background for login page.
// Example: const CUSTOM_LOGIN_PAGE = 'https://yourdomain.com/custom_login.html'
const CUSTOM_LOGIN_PAGE = '';
const VERIFIER = 'https://api.avsecure.dev/verifier/v1';
const GEO = 'https://api.avsecure.dev/geo/v1';
const AVSECURE = 'https://avsecure.dev';
const AGE_WALL = 'https://agepass.dev/age_wall.html';
const X_API_KEY = 'WTBqVbckWm5eEH5W0ULjM8Ziz4k9oesw3x5g4yAj'; // put your API Key here
const IP_CHECK_ENABLED = true;
const SKIP_CHALLENGE_GENERATION = true;
// runRequest returns reply content if request succeeded
static function runGetRequest($url) {
$options = array(
'http' => array(
'header' => array(
"Content-type: application/json",
"X-API-KEY: ". self::X_API_KEY),
'method' => 'GET'
)
);
$context = stream_context_create($options);
try {
$content = @file_get_contents($url, false, $context);
if ($content !== false) {
return $content;
}
} catch (Exception $e) {}
return "";
}
// checkIP checks if ip address is outside age controlled area
static function checkIP($ip) {
$url = self::GEO . "/geoip/$ip";
$content = self::runGetRequest($url);
// in case of error treat users as GB users (require age check)
if ($content === "") {
return false;
}
$region = json_decode($content);
if ($region->{'region'} === "GB") {
return false;
}
return true;
}
// getChallenge retrieves challenge signed by content provider
static function getChallenge() {
$url = self::VERIFIER . '/challenge';
return self::runGetRequest($url);
}
static function getCustomLoginParameter() {
if (self::CUSTOM_LOGIN_PAGE === '') {
return '';
}
return '&cpBgUrl=' . urlencode(self::CUSTOM_LOGIN_PAGE);
}
// getRedirectUrl generates redurect url with challenge
public static function getRedirectUrl($url) {
$challenge = self::SKIP_CHALLENGE_GENERATION ? "" : self::getChallenge();
if ($challenge === "") {
return self::AGE_WALL . "?url=" . urlencode($url) . self::getCustomLoginParameter();
}
$opts = json_decode($challenge);
return self::AGE_WALL . "?message=" . urlencode($opts->{'message'}) . "&url=" . urlencode($url) . self::getCustomLoginParameter();
}
// isUserAuthenticated checks if user has valid token in query parameters
public static function isUserAuthenticated($params, $ip, $origin) {
if (!is_array($params) || $params['uuid'] == "") {
if ($ip === 'UNKNOWN') {
return false;
}
if (self::IP_CHECK_ENABLED) {
return self::checkIP($ip);
}
return false;
}
$js_string = json_encode(array('message' => $params['uuid']));
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => array(
"Content-type: application/json",
"X-API-KEY: ". self::X_API_KEY,
"ORIGIN: ". $origin),
'method' => 'POST',
'content' => $js_string
)
);
$url = self::VERIFIER . '/verify';
$context = stream_context_create($options);
try {
$content = @file_get_contents($url, false, $context);
if ($content !== false) {
return true;
}
} catch (Exception $e) {}
return false;
}
}
?>