AzureEdge SAS Token Expired - JAVA

Lim Kim Chang (AquaEasy) 6 Reputation points
2022-08-04T03:07:51.413+00:00

Hi Guys,

In our Java gateway application, we facing a painful of NOT having the control of the SAS token expire time in azure sdk iot device library

Those class like ClientOptions is final class which stopped us to override the value of 3600 sec (1 hour) to 24 hours

It cause the us the painful of pushing configuration to gateway and caused the frequency of timeout

I need some help and guide on this, how can we extend the SAS token expire time in application level

228018-image.png

Azure IoT
Azure IoT
A category of Azure services for internet of things devices.
400 questions
Azure IoT SDK
Azure IoT SDK
An Azure software development kit that facilitates building applications that connect to Azure IoT services.
222 questions
{count} vote

1 answer

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 15,886 Reputation points Microsoft Employee
    2022-11-08T16:09:17.837+00:00

    Hi @Lim Kim Chang (AquaEasy) ,

    Thank you for posting this question. I am following up to see if you are still stuck with this issue or arrived at a solution. Please let us know if you still need assistance on this issue and we would be glad to help you.

    Here is a resource, Control Access with SAS, I found where you can generate SAS tokens by setting the custom expiration time.

    Please find the below code snippet for reference

    public static String generateSasToken(String resourceUri, String key) throws Exception {  
            // Token will expire in one hour  
            var expiry = Instant.now().getEpochSecond() + 3600;  
      
            String stringToSign = URLEncoder.encode(resourceUri, StandardCharsets.UTF_8) + "\n" + expiry;  
            byte[] decodedKey = Base64.getDecoder().decode(key);  
      
            Mac sha256HMAC = Mac.getInstance("HmacSHA256");  
            SecretKeySpec secretKey = new SecretKeySpec(decodedKey, "HmacSHA256");  
            sha256HMAC.init(secretKey);  
            Base64.Encoder encoder = Base64.getEncoder();  
      
            String signature = new String(encoder.encode(  
                sha256HMAC.doFinal(stringToSign.getBytes(StandardCharsets.UTF_8))), StandardCharsets.UTF_8);  
      
            String token = "SharedAccessSignature sr=" + URLEncoder.encode(resourceUri, StandardCharsets.UTF_8)  
                    + "&sig=" + URLEncoder.encode(signature, StandardCharsets.UTF_8.name()) + "&se=" + expiry;  
                  
            return token;  
        }  
    

    Let us know if this helped you.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.