So now that we know that everything seems to be connected properly, let's generate a token and we're
of course, we're going to initiate that request here in Handler's API right before we generate and
send the payload.
So how are we going to generate the token?
Well, to start with, I'm going to create a new file in my model's folder.
Now I'm putting it in my model's folder because I might at some point in the future, need to have access
to this functionality from some other application that uses this same code base as a starting point.
So I'll create a new file in there in models called Tokens Go.
And it will be package models and what I'm going to do, first of all, is declare constant and I'll
explain why this exists before too long and I might have more than one constant.
So I'll put it this way.
I'm going to call this scope authentication, and that will be equal to the string authentication.
I quite often when you're working with an API, you'll have different kinds of scope.
And it's generally considered good practice to identify the scope for some particular piece of your
code.
And of course, right now we just have one scope authentication, but we might have more later on.
So I'm going to declare that now I'll declare a type and this type will be called token and it is a
structure.
And this is what we're going to store our token in this type.
So the first field will be plaintext and this will be the actual token itself and it will be a string
and it will be in JSON called Token, because that's more meaningful to the end user.
It's also going to have a user ID and that will be the idea of the user from the database.
I'm going to make this one in sixty four because that's what Jason likes to have, but I'm not going
to hand that back in Jason.
OK, so we'll make this nothing in Jason.
I might change that later on.
That's why I'm using it.
Sixty four as the type and the hash which will be a slice of bytes.
And again I'm not going to hand that back in any Jason I generate
next to have the expiry.
How long does this token last and that be a type time.
Time.
And I am going to hand that back with Jason and I'll call that expiry.
And finally, the scope, which will be a string.
And I'm not going to hand that back in Jason either.
OK, so that's my type.
That's format.
This looks good.
It's important time for me.
So we're good.
So the next thing I'll do is create a function and it will be exported because I need to have access
to it from outside of this package.
I'll call that generate token and it will take a few parameters.
It'll take user ID which will be in it.
It will take ttle or time to life, which is time, duration, and it will take the scope, which is
a strength and it will return a pointer to the type we just created, token and potentially an error.
Now let's create a token.
So I'll create a local variable called Token and it will be assigned the value of a reference to token
and we'll populate its fields so its user ID
will be the user already cast to sixty four, which is what this type expects to find.
So our parameter user ID.
It's expiry will be timed now don't add and we'll add the time to life which you received as a parameter
to this function, and the scope will be the scope which we also received as a parameter in this function.
So now I have this variable.
The next thing I need to do is to make sure that this token is going to be secure.
And one way to do that is by assigning some random bytes.
So I'm going to generate a local variable called random bytes
and that will be make slice of byte with the length of 16.
OK, and now I'm going to ignore the first response from the function I'm about to call check for an
error, and that comes from the random package.
Now make sure you get the right one.
Visual Studio code helpfully tells you this one is Krypto Rande.
That is the one I want.
I do not want Matheran, which is not suitable for cryptography.
So Crypto Rande and that should do the import for me.
And I did someone to call Randon read and I'll pass it.
Renovates.
OK, we'll check for an error if error is not equal to nil.
Then return nil in the air.
Something went wrong.
OK, unlikely, but possible.
Now we'll create our token plaintext.
So token plaintext, the actual token we're going to be sending back to the end user and that's going
to be equal to and we want it to be in a very specific format.
OK, so the format is based thirty two, which is right from the encoding package, dot STD encoding,
and I'm going to call with padding on that if necessary, and then it's going to be based thirty to
no padding.
So we're not going to have any padding, not in code to string and we're going to pass that random randomize
and that will generate a hash hash is assigned the value of S.H. A and we want to fifty six right there,
Krypto S.A.G. fifty six and we're going to call some two fifty six and that takes as its parameter casta
a slice of bytes, token dot plain text and that gives us the necessary hash for our token.
OK, and we take that value and stored in token don't hash.
And that's going to be equal to hash
like that, square brackets with a colon in the middle and that gets it in precisely the format we want
it to be.
And then we just return our token return token and no error.
So generally, token is now available to us.
Let's give it a comment.
Generate token, generates a token that lasts for TDL and returns it.
That's sufficient.
OK, so this will generate our token.
We've got to give this a comment too.
And that one is comic.
Tolkan is the type for authentication tokens.
OK, so we've created this and in the next lecture we'll actually use it to generate a token and there's
still a few steps left, but we're getting much closer.