Machine-to-machine authentication #1233
mrcleanandfresh
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I want to be able to do machine-to-machine authentication with user accounts. I believe OAuth 2.0 defines this as "Authorization Code Flow." But I'm not using OAuth, it's an example of what I'm looking for: machine-to-machine authentication using a token. So I created a new class that implements
AuthenticationServicecalledAccountsToken.I just have one method
authenticatein it, and that method is almost an exact copy of theauthenticatefrom@accounts/password'sAccountPassword#authenticatemethod. I finished the implementation, and was able to copy most of the unit tests fromAccountsPasswordand modifying them slightly. So it took me about a day to do the unit testing, and implementation and now I've got a whole new service in my accounts app! I'd share it, but a better example would be looking at theauthenticateandpasswordAuthenticatormethods fromAccountsPasswordthat's what I used, including the way errors were handled.Then, since I'm using the GraphQL package, I made a request to it like so:
One thing of note:
Even though the source code for
AccountsPassword#authenticateindicatesusercan be either astringor anLoginUserIdentity:This is not true, since the GraphQL API expects that it be
UserInputwhich most closely mirrors theLoginUserIdentitytype, although not exactly:Thought I'd share that here, since I found that out while implementing my token service. Something to keep in mind if you're wondering why you're seeing user as a string or object in the source, then are unable to do the same in GraphQL.
I really, really enjoyed the flexibility of being able to create a new service and bolt it on to AccountsJS! It worked really well, and felt nice from an implementation standpoint. You just implement the
AuthenticationServiceand it gives you only about 4 things to worry about: two instance vars and two methods. Pretty simple, extensible and powerful! Then, because I'm using Fastify, I created a plugin:I noticed that the type augmentation will work anywhere (in Fastify), but I liked it co-located and isolated with my plugin. Unfortunately, I couldn't isolate the
verifyPasswordfunction, but I don't plan on creating a package, so it's fine. Plus, myaccounts-passwordplugin shares thatverifyPasswordfunction.Final step is to wire it all together in
AccountsServer:Versions
Beta Was this translation helpful? Give feedback.
All reactions