 Now it's time to look into the OpenPego token, but before starting just to recap, the OpenPego token is just a way to encode a value into a token on a server, then entering that token on a device using the keypad to check if the token is valid, then use that value accordingly. So in this session, before diving into the code, we'll be talking about the different key components that are needed in order to use the OpenPego token, but also we'll talk about different modes that can be used while generating the token, and then we'll see how we can set up those devices like the different parameters that are needed to set those components. So in order to generate a token, we need a server which can be any software provider or any custom product that any manufacturer would like, and this is where the token will be generated. And then that token will be entered on a specific device, and those are the main components that we need. We need the server and we need the device, and the device will have any keypad and then we'll decode the token and then it will use that value. And while generating the token, there are two different modes that can be used. We have the set time and add time. Add time is used when you want to add time to the current value on the device. Let's say you have three days on the device and you add two days, which means at the end of the day, you'll have five days left. And set time is used when you want to set the time on the device to a specific time. Let's say we have three days and then you set time to one day, that means at the end of the day, you'll only have one day left. And in order to set up those components, there are different parameters that are needed. Talking about the server, we need the starting code. We also need the key. This is the key that is used to encode and decode the token. So it's important to remember that while setting up the server, the key should be encoded in hexadecimal format. So we'll see it also in the code. There is the time divider, which is the division factor that is used on the value. This one comes in handy when you want to use a value. Let's say you want to use this token for just a number of hours. Instead of the whole day, you just use a number of hours and then we'll use the time divider. There is the second digit mode. And this one, if it's set to one, then the token will have a digit between one and four. Otherwise, you'll have digits between zero and nine, which means this one is useful depending on the key part used on the device. If the key part has only four digits, then you'll use this one. Then on the server, we keep also the count. And this one is just a value that keeps on incrementing. Whenever a new token is generated on the server, when you generate a new token, the count keeps on incrementing. Same thing for the device. But here, let's see, we need the serial number. This one is not really used during the token generation, but yeah, we know that it's important for every manufacturer to handle serial numbers for their devices. We have the starting code as well. We have the key, which should be in hexadecimal format. We have the time divider as well. And we have the second digit mode. And we have the count. And there is also the test code. This test code is like after setting up the device, let's say you want to test it for a couple of minutes, for a couple of seconds. That's where this one will be used. And every manufacturer will have their own way to implement this use case. So as we just talked about the parameters, now I think it's time we see how we can use them and how quickly we can generate the token using the OpenPego token. So yeah, the OpenPego token code is on GitHub. You can find it there and you can clone it. You can also download it and then use it in your device and on the device and on the server. And it has also some information that might be needed. Also, it contains also documentations that will be useful. And in my case, I've already downloaded the code. As you can see, I've already downloaded the code. And I've also, I assume that my device is ready and my server is ready so that I can go straight to see how we can generate the token. As I mentioned earlier, it's important to remember that the key should be encoded as a decimal so that it can be sent as bytes. So probably in teams, you might want to share it as a string, but then while setting it on the device or on the server, it's better to encode it. That's why you can see I did it as well. Now, let's try to run this code and see as we can see my device is up and running and my server is also up. So which means we can proceed and then we start the first process to generate the token. So let's go on the server and create a method to generate the token. We'll be giving it a value that we want to encode in the token. And then we'll pass also the mod, as I mentioned earlier, which can be either add time or set time. And in order to encode the token, we'll use the encoder, which has the functionality to generate the token. And then we use the method called generate standard token. This method needs the starting code. So we pass the server starting code. It needs the key, which is the server key. And it needs the value, which means like the value that we want to encode into the token. It needs the count, which is the server count in order to keep incrementing the count on the server. And it needs the mod. So we pass also the mod that we'll be using and the restricted digit set to make sure that we generate token depending on the device for each device. And the generate standard token returns two different values. We have the count. It returns the count. So we need to update it. We need to update our server count. And it returns also the token. And let's also return our token that has been generated. This function will generate a token. Let's see if we can get it. So we go on the server. Then we generate the token. Let's see if we pass five as value. Then the mod, the OpenPego token code base has some shared values that will be useful when it comes to the mod. And from there, you can, it's try to use the first one, token type. Then we use the add time. Then let's print our token. And if you run the code, you can see that our token is being generated. So which means everything is good. Now the remaining step is to the code to verify it on the device and then use that value on the device. So let's go on the device and write another method that we'll use to decode. Let's say after the user has entered the value using the keypad and then you want to decode it on the device. Let's say decode token. So it will be receiving the token itself. Okay. And now in order to decode the token, we need the decoder from the OpenPego token. Then get activation value count and type from token. So this function, it needs the token and it's need the starting code. So we pass off the device starting code. Then we pass the key, which is the device key. And we pass the, it's need also the last count, which is the device last count. And it's need the set, which is the device and the used count. Normally, while configuring my device, I created a separate list that will be tracking all the used count and the used token so that in the future, to make sure that if your token has already been used, we don't use it anymore. The decoding the token will be able to handle it. So that should be the use of this used count. So we pass the used count there. Then yeah, I think that's it. So the get activation value returns three different values. We have the token value. I mean the value that has been recorded in the token and it returns the token count and it returns the token type and it returns the token type. Now, after decoding the token, you need to check if it is valid. So if the token is not valid, but if the token is not valid, the token value will be none. So that means like if it's not there, we just say that it's invalid and we can return felt. And then every manufacturer will have their own way probably to prevent the user from entering invalid token over and over again. And then you can handle it in this case. Then the next thing is if a token has already been used, then the token value will be equal to minus two. If the token value is equal to minus two, that means like the token has already been used in the past. So we can say it's an old token. And then otherwise, it means like everything is good. So we can proceed and use the value as you want on the device. For example, in my use case, I was encoding, let's say five, I'm coding days into the token. So let's try to use the value that we have on the device. The first thing that we'll do is to make sure that we are basing also the count on the device to make sure that the server and the device are synchronized. So we give it the token count. The second one is we will update the used count to make sure that after using this token, we don't use it anymore. And to update the used count, there is a function in the decoder that will help us to do that. So the update use count and what it means is just the past use count. So we pass the device use count. It's need the value, which means like the value that we got from the token. And it's need the new count, which is the current count that we got from the device from the token. All right. Now we have updated the count and the used count. Now let's try to use the token. In my use case, I will be setting the expiration date on the device. I will be setting the expiration date on the device depending on the number of days that have been used. So I'm going to create just another method for that. And here I will be passing the token value and the mod to make sure that I handle the both cases correctly. And the first thing that we need to do is to get the number of days because, as I mentioned, we are using, in my use case, I'm using number of days. And here we have the token value. And remember to make sure that you're getting correct number of days. This is where we'll use the time divider to make sure that if I want, if the time divider is two, then we'll divide the value into that was encoded by two. Then here, the next thing is to use the token depending on both use cases. Let's start with the, if it's set time, we also use the shared values. We get the token type and set time. Yeah, so if it's set time, then that means I would just update the expiration date on my device to the current date. Then I add the new number of days. Otherwise, I mean, we are adding, then we just say expiration date is, we get the current one and we add this. And sometimes the expiration date might be already in the past. So it's better to make sure that it is updated so that we have, we have collected value. That's why we need to check if it, if it's already in the past, we just set it to the current one. Yeah. Then these updates, device status will update the device, will update our device and we can use it to make sure that so we give it the token value and we give it the token type. Okay. Now, one thing that I forgot to mention is while decoding the token, you need to change its type to integrals because when you, when you generate a token, it's returned, it's being returned as a string. So when you want to decode it, you need to change it to numbers, then you can decode it. Now, in our test code, we have our token here. Let's, let's use it on the device and see what, what will be the output. So let's also, let's also get the status of our device and the server as well. Then now, this is the initial status. Here we generate the token and here we decode it. We're using it on the device and let's run the code again. Okay. We are having a exception. Okay. Saying that the decode, we didn't pass the type on the update. Okay. Yeah. We forgot to pass something on the update just count. So we pass the token type that was used. Now, we run the code again. Yeah. As you can see, initially our expiration date was set to today, the 15th. And after decoding, after decoding the token, it is now set to the 20th, which means like the token was used correctly and the value which it was five days was also used. I was also decoded correctly and the value was used to set up, to be set on the device. Now, we did an implementation to see if a token cannot be used. Let's see if it's working. So let's just say device dot decode. Let's try to decode this token twice and see if that implementation is working as expected. Yeah. As you can see here, at the end, we have an old token, which means you cannot reuse a token twice or more than once, I mean, because the token value is being returned. The value is minus two and our code is working perfectly. Yeah. This is in short how quickly you can generate a token and decode it using the OpenPego token without having to handle the code yourself, like writing the implementation yourself just by using the code that has been shared. And if you want to learn more about how you can use it and how you can set up different things on the device, in the code base, there is a directory called simulators and there we have a device simulator, which has many other advanced functionalities that you can also set up on your device. Same thing for the server. It also has some many advanced functionalities that you can also use on your server. Just in the code base, you can check them. It also has some examples and it also has some tests that you can check. And while implementing the OpenPego token, if you need help, you can just go to the OpenPego website and then in the documentation section, you just go to the Outlaw, some free help in there and access will reach out to you for help. Thank you, everyone. We just talked about the different components that can be used. I mean the device and the server, we just talked about the different mode that you can use while generating the token, but also we just saw how quick and easy it is to generate a token and decode it on the device. Thank you, everyone.