<commit_before><?php

namespace App\Http\Controllers\Login;

use App\Exceptions\FacilityNotFoundException;
use App\Facility;
use App\Helpers\RatingHelper;
use App\Helpers\RoleHelper;
use App\Helpers\ULSHelper;
use App\Role;
use App\ULSToken;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Jose\Component\KeyManagement\JWKFactory;
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Core\Converter\StandardConverter;
use Jose\Component\Core\JWK;
use Jose\Component\Signature\Algorithm\HS256;
use Jose\Component\Signature\Algorithm\HS384;
use Jose\Component\Signature\Algorithm\HS512;
use Jose\Component\Signature\JWSBuilder;
use Jose\Component\Signature\Serializer\CompactSerializer;

/**
 * Class ULSv2Controller
 * @package App\Http\Controllers\Login
 */
class ULSv2Controller extends Controller
{
    public function getLogin(Request $request) {
        if (!$request->has('fac')) {
            abort(400, "Malformed request");
        }

        $facility = Facility::find($request->get('fac'));
        if (!$facility || !$facility->active) {
            throw new FacilityNotFoundException("Invalid facility");
        }

        if ($facility->uls_jwk == "" || ($facility->uls_return == "" && $facility->uls_devreturn)) {
            abort(400, "Facility is not ready for ULSv2. Please contact the facility webmaster at " . strtolower($facility->id) . "-<EMAIL>");
        }

        session(['fac' => strtoupper($request->input('fac'))]);
        if ($request->has('dev')) {
            session(['dev' => true]);
        }

        return redirect(env('ULSv2_LOGIN'));
    }

    public function getRedirect(Request $request) {
        if (!$request->session()->has('fac')) {
            abort(400, "Malformed request");
        }

        $facility = Facility::where("id", $request->session()->get("fac"))->first();
        $redirect = null;
        if ($request->session()->has("dev")) {
            $request->session()->forget("dev");
            $redirect = $facility->uls_devreturn;
        } else {
            $redirect = $facility->uls_return;
        }
        $facility_jwk = json_decode($facility->uls_jwk, true);

        if (!\Auth::check()) {
            return redirect("$redirect?cancel");
        }

        $algorithmManager = AlgorithmManager::create([
            new HS256(), new HS384(), new HS512()
        ]);
        $jwk = JWK::create(json_decode($facility->uls_jwk, true));
        $jsonConverter = new StandardConverter();
        $jwsBuilder = new JWSBuilder(
            $jsonConverter,
            $algorithmManager
        );

        $data = ULSHelper::generatev2Token(\Auth::user(), $facility);
        $payload = $jsonConverter->encode($data);
        $jws = $jwsBuilder->create()->withPayload($payload)->addSignature($jwk,['alg' => $facility_jwk['alg']]);
        $serializer = new CompactSerializer($jsonConverter);
        $token = $serializer->serialize($jwk, 0);

        $request->session()->forget("fac");

        if ($redirect) {
            return redirect("$redirect?token=$token");
        } else {
            abort(500,"Facility doesn't have a return URL configured");
        }
    }
<commit_msg>ULSv2 use JWS rather than JWK<commit_after><?php

namespace App\Http\Controllers\Login;

use App\Exceptions\FacilityNotFoundException;
use App\Facility;
use App\Helpers\RatingHelper;
use App\Helpers\RoleHelper;
use App\Helpers\ULSHelper;
use App\Role;
use App\ULSToken;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Jose\Component\KeyManagement\JWKFactory;
use Jose\Component\Core\AlgorithmManager;
use Jose\Component\Core\Converter\StandardConverter;
use Jose\Component\Core\JWK;
use Jose\Component\Signature\Algorithm\HS256;
use Jose\Component\Signature\Algorithm\HS384;
use Jose\Component\Signature\Algorithm\HS512;
use Jose\Component\Signature\JWSBuilder;
use Jose\Component\Signature\Serializer\CompactSerializer;

/**
 * Class ULSv2Controller
 * @package App\Http\Controllers\Login
 */
class ULSv2Controller extends Controller
{
    public function getLogin(Request $request) {
        if (!$request->has('fac')) {
            abort(400, "Malformed request");
        }

        $facility = Facility::find($request->get('fac'));
        if (!$facility || !$facility->active) {
            throw new FacilityNotFoundException("Invalid facility");
        }

        if ($facility->uls_jwk == "" || ($facility->uls_return == "" && $facility->uls_devreturn)) {
            abort(400, "Facility is not ready for ULSv2. Please contact the facility webmaster at " . strtolower($facility->id) . "-<EMAIL>");
        }

        session(['fac' => strtoupper($request->input('fac'))]);
        if ($request->has('dev')) {
            session(['dev' => true]);
        }

        return redirect(env('ULSv2_LOGIN'));
    }

    public function getRedirect(Request $request) {
        if (!$request->session()->has('fac')) {
            abort(400, "Malformed request");
        }

        $facility = Facility::where("id", $request->session()->get("fac"))->first();
        $redirect = null;
        if ($request->session()->has("dev")) {
            $request->session()->forget("dev");
            $redirect = $facility->uls_devreturn;
        } else {
            $redirect = $facility->uls_return;
        }
        $facility_jwk = json_decode($facility->uls_jwk, true);

        if (!\Auth::check()) {
            return redirect("$redirect?cancel");
        }

        $algorithmManager = AlgorithmManager::create([
            new HS256(), new HS384(), new HS512()
        ]);
        $jwk = JWK::create(json_decode($facility->uls_jwk, true));
        $jsonConverter = new StandardConverter();
        $jwsBuilder = new JWSBuilder(
            $jsonConverter,
            $algorithmManager
        );

        $data = ULSHelper::generatev2Token(\Auth::user(), $facility);
        $payload = $jsonConverter->encode($data);
        $jws = $jwsBuilder->create()->withPayload($payload)->addSignature($jwk,['alg' => $facility_jwk['alg']]);
        $serializer = new CompactSerializer($jsonConverter);
        $token = $serializer->serialize($jws, 0);

        $request->session()->forget("fac");

        if ($redirect) {
            return redirect("$redirect?token=$token");
        } else {
            abort(500,"Facility doesn't have a return URL configured");
        }
    }

{'fraction_non_alphanumeric': 0.15654060756354618, 'fraction_numerical': 0.011314321140731555, 'mean_word_length': 2.9588957055214724, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '125473', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1872, 'n_words': 347}