Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
joetannenbaum committed Feb 17, 2023
1 parent 176c799 commit df685fd
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 40 deletions.
18 changes: 14 additions & 4 deletions app/Auth/CredentialSourceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class CredentialSourceRepository implements PublicKeyCredentialSourceRepository
{
public function findOneByCredentialId(string $publicKeyCredentialId): ?PublicKeyCredentialSource
{
$authenticator = Authenticator::where('credential_id', base64_encode($publicKeyCredentialId))->first();
$authenticator = Authenticator::where(
'credential_id',
base64_encode($publicKeyCredentialId)
)->first();

if (!$authenticator) {
return null;
Expand All @@ -23,16 +26,23 @@ public function findOneByCredentialId(string $publicKeyCredentialId): ?PublicKey

public function findAllForUserEntity(PublicKeyCredentialUserEntity $publicKeyCredentialUserEntity): array
{
return User::with('authenticators')->where('id', $publicKeyCredentialUserEntity->getId())->first()->authenticators->toArray();
return User::with('authenticators')
->where('id', $publicKeyCredentialUserEntity->getId())
->first()
->authenticators
->toArray();
}

public function saveCredentialSource(PublicKeyCredentialSource $publicKeyCredentialSource): void
{
$user = User::where('id', $publicKeyCredentialSource->getUserHandle())->firstOrFail();
$user = User::where(
'id',
$publicKeyCredentialSource->getUserHandle()
)->firstOrFail();

$user->authenticators()->save(new Authenticator([
'credential_id' => $publicKeyCredentialSource->getPublicKeyCredentialId(),
'public_key' => $publicKeyCredentialSource->jsonSerialize(),
'public_key' => $publicKeyCredentialSource->jsonSerialize(),
]));
}
}
10 changes: 7 additions & 3 deletions app/Http/Controllers/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Psr\Http\Message\ServerRequestInterface;
use Webauthn\AttestationStatement\AttestationObjectLoader;
Expand Down Expand Up @@ -41,10 +40,15 @@ public function generateOptions(Request $request)
'username' => $request->input('username'),
]);

if (!$user->exists) {
$user->save();
if ($user->exists) {
// We're in registration mode, they shouldn't be able to register a new device to an existing user
throw ValidationException::withMessages([
'username' => 'Username already exists',
]);
}

$user->save();

$userEntity = PublicKeyCredentialUserEntity::create(
$user->username,
$user->id,
Expand Down
4 changes: 4 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],

'fathom_analytics' => [
'id' => env('FATHOM_SITE_ID'),
],

];

This file was deleted.

24 changes: 23 additions & 1 deletion resources/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ document.addEventListener('alpine:init', () => {
Alpine.data('authForm', () => ({
mode: 'login',
username: '',
name: '',
browserSupported: browserSupportsWebAuthn(),
error: null,
submit() {
Expand All @@ -22,14 +21,22 @@ document.addEventListener('alpine:init', () => {
return this.submitRegister();
},
submitRegister() {
this.trackEvent('register-start');

window.axios
// Ask for the registration options
.post('/registration/options', {
username: this.username,
})
// Prompt the user to create a passkey
.then((response) => startRegistration(response.data))
// Verify the data with the server
.then((attResp) => axios.post('/registration/verify', attResp))
.then((verificationResponse) => {
if (verificationResponse.data?.verified) {
// If we're good, reload the page and
// the server will redirect us to the dashboard
this.trackEvent('register-complete');
return window.location.reload();
}

Expand All @@ -41,16 +48,24 @@ document.addEventListener('alpine:init', () => {
});
},
submitLogin() {
this.trackEvent('login-start');

window.axios
// Ask for the authentication options
.post('/authentication/options', {
username: this.username,
})
// Prompt the user to authenticate with their passkey
.then((response) => startAuthentication(response.data))
// Verify the data with the server
.then((attResp) =>
axios.post('/authentication/verify', attResp),
)
.then((verificationResponse) => {
// If we're good, reload the page and
// the server will redirect us to the dashboard
if (verificationResponse.data?.verified) {
this.trackEvent('login-complete');
return window.location.reload();
}

Expand All @@ -69,5 +84,12 @@ document.addEventListener('alpine:init', () => {
this.error = error?.response?.data?.message || error;
});
},
trackEvent(eventId) {
if (typeof fathom === 'undefined') {
return;
}

fathom.trackGoal(eventId, 0);
},
}));
});
5 changes: 5 additions & 0 deletions resources/views/layout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

@vite(['resources/css/app.css', 'resources/js/app.js'])
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>

@if (config('services.fathom_analytics.id'))
<script src="https://cdn.usefathom.com/script.js" data-site="{{ config('services.fathom_analytics.id') }}" defer>
</script>
@endif
</head>

<body class="h-full">
Expand Down

0 comments on commit df685fd

Please sign in to comment.