Blog

  1. I totally lost the ball on blogging recently. I have been making some changes to my site recently and it is not as easy to post now and therefor I post less. I'm hoping to have a little more time soon to get a few kinks worked out.

    I'm considering changing a few things up in regard to the way I blog and hopefully start having posting some more coherent thoughts.

    6:09pm on November 13, 2022
  2. Experiment: Work Schedule

    After reading this post from Mike Crittenden on experimenting on myself, I decided to run my own experiment this past week.

    I asked my boss if it would be ok if I started work at 7am instead of my usually 8am and then take an extra hour off for lunch. This gave me an hour and I half off for lunch and it was super nice! Not only did it give me a break from siting at a desk all day but it also gave me a chance to get a few things done.

    I'm going to talk to my boss and if he is ok with it, I think I'm going to make this my new schedule.

    11:12pm on November 5, 2022
  3. I started working on redesigning my site today. I just wanted to get something up previously so I neglected the design. I will hopefully have that fixed soon.

    7:40pm on October 16, 2022
  4. Choosing the Long Line

    Something interesting happened the other day. I was going through the Chick-fil-a drive-through and there had two lanes. One of the lines had a significant line while the other one had no cars in it. The way it wraps around the building, I could not tell if their where people working both lanes or just the one. And so I wondered, is this second lane open, and if so, why were was no one in that lane. It took me a moment to try to decide what line I should enter.

    It was fascinating, because I chose to enter the line of cars in lane one as opposed to taking the risk of entering lane two and it turning out to be closed. I later approached the area where you place you order and realized both lanes were in fact open. Yet, in fear that I would end up in a lane that was closed, I opted to join the long line and play it safe.

    Just and interesting observation. I'm sure their have been plenty of people that have studied this.

    10:35pm on October 15, 2022
  5. Migrating from Laravel's Token Guard to Sanctum

    In an application I was working on today, I had to migrate from Laravel token authentication to Sanctum. The process went relatively smoothly but just in case someone else has trouble, I thought I would layout the migration process.

    Installing Sanctum

    I’m going to direct you to the official documentation for instructions on installing Sanctum. It’s very straight forward and should be easy for anyone follow.

    Migrating Api Keys

    In this application, we had a ApiUser model to distinguish between regular users and other applications that talk to our api. While Sanctum uses a second table to store the access tokens (personal_access_tokens), with the Laravel token authentication guard, we were storing the token directly on the ApiUser model itself. We did not want to invalidate all of the api keys we already had in the database so migrating them all over to Sanctum’s personal_access_tokens table was essential.

    Following the documentation, I added the HasApiTokens trait to the model I was going to be associating the tokens with. This provides all of the necessary methods and relationships in order to both create tokens for the model and to authenticate the incoming request against the model.

    To migrate the tokens from the old table to the new one was a little bit tricky and I had to do some digging in the source code of Sanctum in order to come up with a solution. You see, we were storing our tokens in plain text in the database, but Sanctum hashes the token before storing them. This meant it was not just as simple as coping the tokens from one table to the next.

    I first tried using the createToken() method the HasApiTokens trait provides. The problem with this is was, it generates the token for you and does not allow you to actually specify you own string to use as the token (which is what I needed to do in order to migrate the old tokens over).

    I was able to dive into the source code for createToken() and realized all it was doing was creating a PersonalAccessToken with a randomly generated string and saving it to the model. I copied and pasted this function and with a little modification, was able to create this migration that would work.

    return new class extends Migration
    {
        public function up()
        {
            ApiUser::all()->each(function (ApiUser $apiUser) {
                $plainTextToken = $apiUser->api_token;
    
                $apiUser->tokens()->create([
                    ‘name’ => ‘Migrated Token’,
                    ‘token’ => hash(‘sha256’, $plainTextToken),
                    ‘abilities’ => [‘*’],
                    ‘expires_at’ => null,
                ]);
            });
        }
    };
    

    Changing the Authentication Middleware

    I had to then instruct Laravel to use Sanctum as opposed to the token guard for authentication to our api. I did a search for ->middleware([‘auth:api’]) and replaced all instances of auth:api with auth:sanctum.

    Updating Tests

    With the all of the tokens successfully migrated over to the personal_access_tokens table and our api.php routes file updated to use the new middleware, the last thing to do was to update all of our tests that were manually passing a bearer token in the authorization header with every request to using this snippet that I found and modified from the documentation: Sanctum::actingAs(ApiUser::factory()->create(), [‘*’]);.

    Conclusion

    In conclusion, migrating to Laravel Sanctum is not as hard as I expect it would be. It’s fairly straight forward with the hardest part being the migration of old api tokens.

    I hope this article will be helpful to you as a guide for the process of migrating to Sanctum.

    12:01pm on October 13, 2022
  6. Today at work, we had some tests failing in CI all of the sudden for no apparent reason. It turns out, it had something to do with the fact that it's October and one of our tests were failing because of it.

    10:03pm on October 12, 2022
  7. This weekend, I went camping with some friends and my dad and brother. We went to Overland Expo and here are some photos I got from it.

    9:05pm on October 9, 2022
  8. Replacing the Heater Core on my 2001 Jeep Cherokee XJ

    I have had the sneaky suspicion that my heater core on my Jeep has been leaking for a little while now and have been waiting for a good time to replace it. With a camping trip coming up, I finally decided to tackle it. I had read online that it would take roughly 5-10 hours to complete so on Saturday evening, I began the process of completely taking the dash apart.

    It took me Saturday night, most of the day Sunday, and the past several evenings after work to get the job done. And boy was it a big job! I would say I have ~15hrs into it but that also includes flushing out the entire coolant system.

    I'm super glad to have finally have heat that does not result in all the winds fogging up.

    Oh... and it rain all weekend while I was working outside.

    On a side note, I went ahead and decided to replace the thermostat because I wanted to remove the housing to properly flush out the engine. These Jeep's take a 180 degree thermostat but it is not uncommon for people to unintentionally, or without knowing better, replace them with a 195 degree and that was the case with my Jeep. Not to mention, some how a o-ring was suck in the thermostat.

    With a freshly flushed coolant system and a new thermostat, the Jeep is staying nice and cool.

    9:09pm on October 5, 2022