All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.microsoft.azure.servicebus.security.SharedAccessSignatureTokenProvider Maven / Gradle / Ivy

Go to download

Java library for Azure Service Bus. Please note, a newer package com.azure:azure-messaging-servicebus for Azure Service Bus is available as of December 2020. While this package will continue to receive critical bug fixes, we strongly encourage you to upgrade. Read the migration guide at https://aka.ms/azsdk/java/migrate/sb for more details.

There is a newer version: 3.6.7
Show newest version
package com.microsoft.azure.servicebus.security;

import java.security.InvalidKeyException;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.CompletableFuture;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.microsoft.azure.servicebus.primitives.MessagingFactory;
import com.microsoft.azure.servicebus.primitives.SASUtil;

/**
 * This is a token provider that generates Shared Access Signature(SAS) tokens or reuses an already generated SAS token.
 * @since 1.2.0
 *
 */
public class SharedAccessSignatureTokenProvider extends TokenProvider
{
    private static final Logger TRACE_LOGGER = LoggerFactory.getLogger(SharedAccessSignatureTokenProvider.class);
    
    private String sasKeyName;
    private String sasKey;
    private int tokenValidityInSeconds;
    private String sasToken;
    private Instant sasTokenValidUntil;
    
    /**
     * Creates an instance that generates SAS tokens from the given SAS key name and value.
     * @param sasKeyName name of the SAS key
     * @param sasKey SAS key
     * @param tokenValidityInSeconds validity of the token to be generated
     */
    public SharedAccessSignatureTokenProvider(String sasKeyName, String sasKey, int tokenValidityInSeconds)
    {
        this.sasKeyName = sasKeyName;
        this.sasKey = sasKey;
        this.tokenValidityInSeconds = tokenValidityInSeconds;
        this.sasToken = null;
    }
    
    /**
     * Creates an instance that doesn't generate tokens but reuses an already generated SAS token.
     * @param sasToken SAS token already generated
     * @param sasTokenValidUntil Instant when the SAS token expires.
     */
    public SharedAccessSignatureTokenProvider(String sasToken, Instant sasTokenValidUntil)
    {
        this.sasToken = sasToken;
        this.sasTokenValidUntil= sasTokenValidUntil;
    }
    
    @Override
    public CompletableFuture getSecurityTokenAsync(String audience) {
        if(this.sasToken != null)
        {
            SecurityToken securityToken = new SecurityToken(SecurityTokenType.SAS, audience, this.sasToken, Instant.now(), this.sasTokenValidUntil);
            return CompletableFuture.completedFuture(securityToken);
        }
        else
        {
            CompletableFuture tokenGeneratingFuture = new CompletableFuture<>();
            MessagingFactory.INTERNAL_THREAD_POOL.execute(() -> {
                try {
                    String genereatedSASToken = SASUtil.generateSharedAccessSignatureToken(this.sasKeyName, this.sasKey, audience, this.tokenValidityInSeconds);
                    tokenGeneratingFuture.complete(new SecurityToken(SecurityTokenType.SAS, audience, genereatedSASToken, Instant.now(), Instant.now().plus(Duration.ofSeconds(this.tokenValidityInSeconds))));
                } catch (InvalidKeyException e) {
                    TRACE_LOGGER.error("SharedAccessSignature token generation failed.", e);
                    tokenGeneratingFuture.completeExceptionally(e);
                }
            });
            
            return tokenGeneratingFuture;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy