YouTubevideo· Aug 1, 2024· 25:13· 2,200 views on YouTube

Building a WorkOS Laravel Socialite Adapter from Scratch (Code with me)

Watch on YouTube ↗
0:00

React at a moment you like. The best moments light up here.

transcript

60 moments
  1. I've always been a big fan of everything that lville gives you out of the box whether it's authentication scaffolding with Breeze or jet stream or interacting with stripe with cashier there's also socialite put simply socialite is just an easier way to interact with ooth providers think like Facebook or GitHub but how hard is it to add a new platform to social light as a new provider or adapter the answer really not that hard so let's build it

  2. together larel social already has providers supported out of the box like Facebook Twitter LinkedIn Google GitHub gitlab bitbucket and slack but there's entirely community-driven website called social life providers that has a ton

  3. more just look at how many there are so how hard is it to contribute to one to create a new provider for a application or a service that doesn't actually exist within this community-driven package this video is sponsored by work OS so we're going to to take some time building a new work OS provider adapter for sociala work OS

  4. gives you an all-in-one solution to add a complete set of complex Enterprise features into your code easily so this includes things like single sign on SCM provisioning ro-based Access Control audit logs and like they say a whole lot more so in this video what we're going to be using is work os's off for all SSO

  5. providers so there's single sign on solution as well as their authkit solution to be able to integrate that into laravel socialite to have SSO out of the box as well as this flexible authentication UI for things like social login why don't we get started so the first thing that we're going to check is how other social providers have used

  6. this social light providers package and how they've created their own package for that specific provider so how can we replicate that for work OS I've already created my own account with in here and you can see there's kind of two pieces that I want to set up the first would be

  7. the authentication piece for authkit and so that's going to be okay can you sign in with Google or GitHub for example and they allow you to have some demo credentials already set up so you don't have to worry about configuring that yourself while you're in staging or while you're kind of creating this it just gives you it out of the box so that's oit and then the second piece would be the single sign on so using things like OCTA Google workspace entra ID with samle and oidc identity

  8. providers and so those are the two pieces that we're going to utilize maybe you have the ability within this integration to say okay on this login page I just want to use oit or maybe I can have my users who have already been set up within an organization Now log into that organization I can pass this organization ID into that login so that they're using single sign on with whatever provider or IDP they've set up

  9. themselves okay creating a provider we open up our terminal why don't we create a new directory we can call that social light provider work OS let's open this up in our code editor looks like we need a provider class a listener Handler and then a composer.

  10. Json which is where we're going to have the social light providers manager package that we're requiring let's take a look look at what other providers have done so for example let's say off zero and open this up okay yeah so there's four files or a read me with three other files the composer Json and then there's a extend social light so looks like this extends that social light provids manager package and then

  11. we also have the provider which I'd assume this is where all of yeah all of the actual API s come from so we need the specific tokens get user by token a lot of this is going to be very similar to how we would set up the work OS provider package but there's of course going to be some differences as well particularly how we're going to have two options if we take a look at the work OS API again one for authentication via SSO

  12. and it looks like you can authenticate a user with authkit or SS so and that's going to be what we're passing in yeah looks like okay you can pass in the provider string and that provider string has either this authkit string or any kind of different provider and then organization ID so in our vs code why don't we make those files so we can have the provider. PHP we can create a new file called workos extend social light.

  13. PHP and that's really similar to what this file has here off zero extend socialite work OS extend socialite and then we'll also create a new composer.

  14. Json perfect why don't I go ahead and copy some of this code and we'll just kind of make the changes that we need should be work OS and SSO provider and we'll put my name here we'll call the autoload as work OS everything looks good there why don't we copy what we need for the uh work OS extend socialite

  15. file that's similar to this file right here and this is going to be where we just extend that socialite providers manager so again a lot of what we're doing right here is just enabling this to be called with that socialite helper function within Layell so that way we can say socialite and use the work OS

  16. package and we'll say this is work OS here we'll set this class as workos extend social light and then everything looks good to go there you'll see we have some errors and this is just because this is a class that hasn't actually been installed this is what we're going to use in our code locally

  17. before we push this up to composer locally in our code so that way we can test is everything working as it should now let's start creating that provider file this is what we can eventually start testing with a new Lille install but let's just go ahead and work with the workos API to see what we need so it looks like what we usually need in this provider is a way to have okay Scopes we

  18. have a scope separator and then we need the base URL and everything like that so let's copy what we can from this other social adapter and then we can build upon that paste it in here and we'll make any necessary changes so it looks like azero uses this base URL in the setting and we don't don't probably need that within work OS so it looks like for in the API it uses this authenticate and

  19. that would probably be for the token so yeah authenticate with code whereas the get authorization URL would be authorize so it looks like there's those two links that we would need first to authorize someone okay let's get that call back code but then we also have this authenticate URL which I would assume is after we receive the uh users code we

  20. can then authenticate them so we have these two different URLs so we should not need to change that or have the user set that in this particular package like other packages and providers might we'll just hardcode these in the provider itself so it looks like the provider in this case the social light providers abstract provider which we're extending in this class it does need to have a token URL and probably this git user by

  21. token function so those are the things that we need to pass into this we have this git token URL which we can just return here so if we turn this as a string we could pass in this authenticate string there then get user by token it looks like this needs a specific authorization Bearer token and in this case we just need the client ID

  22. and client secret that the user is going to pass in let's just actually return the response for now so we'll say this credentials response body CU then the manager's package in the abstract provider of OA 2 we have credentials

  23. response body as an array and it's using this user or in this case instantiating the user and getting the taking the access token and setting the credentials response body to the response so this should be enough enough in this case because the credentials response body does need the token we probably are good to go to leave that there how does this change if at all let's take a look at the API so the response is going to have a first name a last name as well as

  24. organization ID there is no Avatar but there is a profile picture URL so I went ahead and copy those in because the user object returns this or the response returns this user object we need all the parameters from the user object so ID email instead of nickname we have the specific email and then the profile picture URL so those are the things that we're setting to the user when we're returning and mapping this user to a specific object okay that looks good to

  25. go this o URL is probably not the one we want because what we need is this authorized URL up here let's take a look at what this build AU URL from base see yeah doing some digging it looks like the internal build off URL from base method builds the authentication URL with all the necessary parameters so we do need to have that initiated we are just going to pass it in that direct

  26. string instead so we have authorize and then we can pass it in the state to the URL and here's where we can probably say okay do we want to pass it in that authkit parameter because if we take a look in this API reference this is where we can set are we passing in an organization ID or connection ID to tell it that this is an SSO connection or and

  27. yeah use to initiate SSO for an organization or if we're not initiating an SSO connection we'll just use the authkit provider so we can remove this additional config Keys we don't need a base URL we're already setting that we can remove this off zero URL because that's just grabbing it from the config the only thing that we need to set or have some configuration for would then be H we probably need a an organization

  28. ID that we can pass into this string and we also would need a maybe any kind of provider id so instead of authkit we could also use these and we would need this as specific settings that we would want to pass into this off URL parameter so why don't we say that it's going to be authkit by default but if you set a specific provider or an organization ID or even a connection ID in this package

  29. then it will default to that so we can say perhaps there's a protected uh use authkit we can set that to true so it's using authkit by default so that way here if this use oit is true we're going to add to this URL this authorized URL the provider off kit so we should be able to do maybe in we'll say that this is a URL and we can add onto the URL and say that this URL now has and provider

  30. equals o kit that looks good now we can return the URL maybe for clarity sake we'll say that this is the O URL so now we're saying it should use authkit by default for the provider but if any provider is input and so that way we'll have initial method maybe down here to say we can set the provider and modify that URL string as needed so maybe we'll have a public function of with

  31. organization ID and so what we can set here is first we want to say that this authkit or use authkit is false but then we can set the parameters of organization ID to the organization ID that is passed in these parameters will then set organization ID for that response for that request I should say to whatever organization ID we set so

  32. just like this API expects so why don't we do the same thing but we'll do it for that connection ID so we'll say with connection ID and then last but not least we'll do it for a provider as well because you could pass in if oit is

  33. default we could pass in any of these as well so so again these parameters are just what this API expects for that slash authorized so we have everything set up we're actually going to use this repository locally in a new Lille application so then in my code directory which is the directory for my projects I'm going to say Lille new we can say testing work OS the goal with this new

  34. larel application is going to be able to test this new project that we created as a work OS provider so that way we can see in real time is everything working are we receiving the information we need to receive from the provider is there any changes that we need to make once we make the changes it's easy to kind of test it in real time before we actually publish this to composer or in this case create a pull request for the socialite providers package so in that testing work OS directory we created and we open

  35. this up in our code editor then we'll go ahead and install it so the first thing that needs to happen for installing any social provider is we need to install that provider via composer and so how can we do this within a local package well it's pretty simple what we can do in our composer. Json we can say we need

  36. repositories and that's going to be an array with a type of path and then this is where we can pass in that URL so we can say that this is the social light providers work OS so what we can do in our terminal is we can say composer require social life provider slor OS and then we need to pass it a directive saying that this is the dev dependency just because this has not been published yet so again this is going to be identical to uh this psr4 so

  37. now that that's been installed it should be Sim link with this larel application so any changes we make in this directory here we should automatically see in this directory here and looks like there was One Small Change we don't have any docs just yet so now in larvo 11 there's going to be a bootstrap providers.

  38. PHP that we need to add so we can add that there next we need to add an event listener for that particular package in this case we need to add this into our app service provider I'm just going to copy this and make the changes so app service provider in the boot we can add

  39. the event that's the facade and and we can just call this the provider of our installed package so we have this work OS provider that I'm bringing in and why don't we do that for this as well social light was called so we just brought in those dependencies here for social light was called and the work OS provider package that we just installed that we're Sim linking to our local file and now we should be good to go because

  40. we're using this social like providers manager package the environment variables that we're setting should be the same syntax and we shouldn't have to change anything for that so we can pass this in as work OS for the app service provider and then in our services config here's where we can add work OS so we'll go ahead and add work OS and we'll set

  41. these to the variables that we need so we need a client ID a client secret and then a redirect and why don't we actually just for Simplicity and Clarity sake I know in the work OS dashboard the redirect is going to be called redirect URI so why don't we call it that redirect URI so now in our environment variables we can go ahead and add those work OS client ID client secret and redirect URI and we'll set those I've already refreshed these and in our

  42. redirect why don't we set this to our app URL so we're going to have testing work os.

  43. test and what we can say is the default configuration for socialite so that would be SLO workos or the name of provider and then the call back URL so we'll go ahead and set that in our work OS config set it to default now we should be good to go and test this out so in our web routes what we can do is maybe say a route get and we can say the login

  44. and instead of returning a view what we can return is socialite pull that in driver work OS redirect so this should if we've set up everything correctly this should redirect us to the authkit that we have set up in our work OS uh application our work OS dashboard because we're not passing in any parameters we could pass in the with organization ID but we're

  45. just keeping it plain and simple for now and should be using authkit so in testing work .test and we would go to SL login we should see there we go there's our off kit that we've set up so again this is a way that we have now enabled work OS to be enabled within our social

  46. life provider so if I was to log in Via GitHub we don't have a route to call us back just yet yeah not found but you can see here in the URL we have this off work OS call back okay that's directing properly then we have this code first let's just get all the information from here and maybe grab the user in that specific instance or in this case they have been authorized but now we need to authenticate the user so let's go ahead and create a new route we'll say route git and we can say off work OS call back

  47. so first why don't we go ahead and just grab the user and since we've set up a socialite driver what we can do is say we'll say the user equals the socialite driver work OS that we're using and then getting the user so it should because we set that up in our package be able to grab the user from that token and give us the necessary information why don't we go ahead and die and dump the user just to see and yeah there we go we have the ID of the user we have the nickname

  48. again that's things that we have set in our package file here so this is the provider we've set that nickname name email Avatar this is all stuff that we could set uh differently if we wanted to so now we have all the necessary information to actually create a new user in our database based off this we can sync it if we wanted to or in that or in this case just log in the user with this necessary information instead of dying and dumping what we could say is we have the user and we need to

  49. update or create a new user so in this case we need to update or create if we were to take a look at the lelt socialite docs here you can kind of see we have the call back where we get the user from social light yes and then update or create and this is exactly what we're going to do we're going to update or create and then add the information that we're getting from this social light driver so in this case we can update based off of a ID in this

  50. case the provider id is what we really want to set so maybe we have this provider id um or we could even say work OS ID if if that's the only provider you're using within socialite within this application and and we can set that to the user user ID because that's what

  51. we have here the ID is that work OS user ID so that's going to be the one that doesn't change so now it's going to update or create based off this user ID so if that user ID exists then we'll put in this information now we can authenticate with that user so Au login user and why don't

  52. we redirect them to the home route and we'll call this one the home route for time sake we create a new migration that adds all of this behind the scenes so that way we have actual information in our user table which we don't have right now to get all this set up and stored there we go and let's go ahead and migrate this Artisan migrate fresh we have a new user let's log them in

  53. so login okay I'm going to authenticate within GitHub and now we should have our user authenticated great so if I was to have a disal page maybe a dashboard where I displayed some information let's just do that real quick and there we go now we have the name set up again we're authenticating using work OS so how do we use the organization ID this is going

  54. be something that you could then Implement in your application where maybe after someone has signed up with your application you create a new organization with work os's API but then how do you get people to log in based off that organization well maybe it's a subdomain on your app that you pass in that particular organization ID in this case we've set this up within the work

  55. OS social life provider so that we can in our web route instead of calling work OSS user or instead of calling work OS redirect I should say we can say with organization ID and we could pass that in there now we can redirect a user to a

  56. SSO login and perhaps we're grabbing this organization ID from the database this could be something that you could set up with for a multi-tenant app where now you have users who are logging in via S so let's take a look at what this looks like so we'll say slash login and now we have this identity provider and this is just a test that I've set up within work os's data board but we could have its own SSO profile

  57. specific to that organization you can see how this could be incredibly valuable if you have multiple organizations who you need to initiate and instantiate users based off of their Google workspace or OCTA or whatever provider that they might have and being able to have this passed in to this social light driver is incredibly helpful because it can be dynamic it can change but also just having this as a

  58. default of authkit is incredibly helpful too you can see the benefits that something like this could have especially if you move into more enterpris type of users who are expecting something like idps or uh user provisioning within something like OCTA or Google workspace now the social life provider that we created within work OS has the ability to use authkit from work Os or SSO so you can dynamically pass in

  59. that organization ID as you see fit based off of their subscription within your app for for example this just goes to show that there are so many things within the Lille ecosystem that are made incredibly easy even if you have to spend some time configuring and adding new additional ones we didn't have to spend all the time setting up particular parameters or options within our API call it just worked out of the box because we're using socialite and socialite gives us that ability to kind of have that background that that base

  60. for us ready to go so hopefully this gives you an idea of how you can maybe add your own soci provider or get started with building on top of things that laral has already given for you so keep creating \[Music\]

just you here