Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add API Key support (🚧) #350

Open
wants to merge 2 commits into
base: v1.0.0-alpha
Choose a base branch
from

Conversation

grablair
Copy link
Contributor

@grablair grablair commented Jan 18, 2025

Resolves #339

This is a WIP change to add API keys. The feature is implemented and admins can create API keys and they can be used to access the APIs in a granular fashion. I still need to complete all translations, and want to give @daveearley an opportunity to review this sooner rather than later.

This also adds a migration to add account_id to the token table, so tokens can be set up on a per-account basis, and the auth flow knows which account a token is attached to, as we don't have the JWT payload to tell us. (TODO: May need to implement key filtering from view for multiple-account situations)

Tested with expired tokens, tokens with missing abilities, and valid tokens.

Screenshot 2025-01-18 at 09 46 30
Screenshot 2025-01-18 at 09 46 37
Screenshot 2025-01-18 at 09 46 46
Screenshot 2025-01-18 at 09 47 06

Checklist

  • I have read the contributing guidelines.
  • My code is of good quality and follows the coding standards of the project.
  • I have tested my changes, and they work as expected.

Thank you for your contribution! 🎉

@daveearley
Copy link
Contributor

Nice! Thanks for contributing this. I'll set aside some time tomorrow to review it.

@meilechwieder
Copy link

Is there somewhere a swagger documentation or the likes for the API?

Copy link
Contributor

@daveearley daveearley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work here! Thank you very much for the contribution. If it's OK with you, I can use this branch as the base for auth changes and I can push up some changes/fixes.

Model::preventLazyLoading(!app()->isProduction());

Relation::enforceMorphMap([
EventDomainObject::class => Event::class,
OrganizerDomainObject::class => Organizer::class,
'user' => User::class,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious what the reason for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As soon as I added the traits required for Sanctum to the User class, I received an error about being unable to morph a User object (I unfortunately don't recall the specific error). Upon doing some google research, I discovered some other people suggesting this as a fix to the error, and it worked.

I'm sure if you remove this you'll get the same error, just in case there is a better solution I am unaware of. Or maybe you won't and I was just being gaslit. 😂

frontend/src/api/api-keys.client.ts Outdated Show resolved Hide resolved
@@ -24,6 +24,7 @@ const ALLOWED_UNAUTHENTICATED_PATHS = [
'widget',
'/product/',
'check-in',
'csrf-cookie',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This array relates to frontend path. From what I can see csrf-cookie is a backend path.

@@ -20,7 +20,9 @@ export const authClient = {
},

login: async (user: LoginData) => {
const response = await api.post<LoginResponse>('auth/login', user);
const response = await api.get('/csrf-cookie').then(async response => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async response can be removed as it's unused.

export const CreateApiKeyModal = ({onClose, onCompleted}: CreateApiKeyModalProps) => {
const createMutation = useCreateApiKey();
const queryClient = useQueryClient();
const formErrorHandler = useFormErrorResponseHandler();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is unused

const handleCreate = useMutation({
mutationFn: (apiKeyRequest: CreateApiKeyRequest) => apiKeysClient.create(apiKeyRequest),

onSuccess: (response: NewApiKey) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewApiKey doesn't exist

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interface does exist, but you're right, I didn't import it in this file and I didn't specify it as the result of a Create call in the api-keys.client.ts file.

Thanks!

@grablair
Copy link
Contributor Author

Great work here! Thank you very much for the contribution. If it's OK with you, I can use this branch as the base for auth changes and I can push up some changes/fixes.

You're welcome! Thanks for the review. Definitely feel free to use this as a jumping off point for the auth refactor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants