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

com.yubico.core.DefaultSessionManager Maven / Gradle / Ivy

There is a newer version: 1.12.2
Show newest version
package com.yubico.core;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.yubico.webauthn.data.ByteArray;
import lombok.NonNull;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class DefaultSessionManager implements SessionManager {
    private Cache sessionIdsToUsers = newCache();

    private Cache usersToSessionIds = newCache();

    private static  Cache newCache() {
        return CacheBuilder.newBuilder()
            .maximumSize(100L)
            .expireAfterAccess(5L, TimeUnit.MINUTES)
            .build();
    }

    @Override
    public ByteArray createSession(@NonNull ByteArray userHandle) throws ExecutionException {
        if (userHandle == null) {
            throw new NullPointerException("userHandle is marked non-null but is null");
        }
        ByteArray sessionId = this.usersToSessionIds.get(userHandle, () -> SessionManager.generateRandom(32));
        this.sessionIdsToUsers.put(sessionId, userHandle);
        return sessionId;
    }

    @Override
    public Optional getSession(@NonNull ByteArray token) {
        if (token == null) {
            throw new NullPointerException("token is marked non-null but is null");
        }
        return Optional.ofNullable(this.sessionIdsToUsers.getIfPresent(token));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy