// Path: frostillicus-blog/frostillicus-blog-j2ee/src/main/java/security/AccessTokenAuthHandler.java
// @AllArgsConstructor
// public static class AccessTokenAuthenticator extends HttpClient.Authenticator implements Credential {
// 	private static final long serialVersionUID = 1L;
// 
// 	private final @Getter String token;
// 
// 	@SneakyThrows
// 	public String getDn() {
// 		var session = DarwinoApplication.get().getLocalJsonDBServer().createSystemSession(null);
// 		try {
// 			var database = session.getDatabase(AppDatabaseDef.DATABASE_NAME);
// 			var store = database.getStore(AppDatabaseDef.STORE_TOKENS);
// 			var query = JsonObject.of("token", token); //$NON-NLS-1$
// 			var tokenDoc = store.openCursor()
// 				.query(query)
// 				.findOneDocument();
// 			return tokenDoc == null ? null : tokenDoc.getString("userName"); //$NON-NLS-1$
// 		} finally {
// 			session.close();
// 		}
// 	}
// 
// 	@Override
// 	public boolean isValid() {
// 		return StringUtil.isNotEmpty(getDn());
// 	}
// 
// 	@Override
// 	public Map<String, String> getAuthenticationHeaders() {
// 		if(isValid()) {
// 			return Collections.singletonMap(HttpBase.HEADER_AUTHORIZATION, "Bearer " + token); //$NON-NLS-1$
// 		}
// 		return null;
// 	}
// }


import javax.enterprise.context.ApplicationScoped;
import javax.security.enterprise.credential.Credential;
import javax.security.enterprise.credential.UsernamePasswordCredential;
import javax.security.enterprise.identitystore.CredentialValidationResult;
import javax.security.enterprise.identitystore.IdentityStore;
import javax.security.enterprise.identitystore.IdentityStoreHandler;
import com.darwino.commons.Platform;
import com.darwino.commons.security.acl.UserException;
import com.darwino.commons.security.acl.UserService;
import security.AccessTokenAuthHandler.AccessTokenAuthenticator;
/*
 * Copyright © 2012-2020 Jesse Gallagher
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package security;




/**
 * Implements the Jakarta EE security API for {@link AccessTokenAuthHandler} use.
 *
 * @author Jesse Gallagher
 * @since 2.3.0
 */
@ApplicationScoped
public class AccessTokenIdentityStore implements IdentityStore, IdentityStoreHandler {
	public static final String STORE_ID = AccessTokenIdentityStore.class.getName();

// Path: frostillicus-blog/frostillicus-blog-j2ee/src/main/java/security/AccessTokenAuthHandler.java
// @AllArgsConstructor
// public static class AccessTokenAuthenticator extends HttpClient.Authenticator implements Credential {
// 	private static final long serialVersionUID = 1L;
// 
// 	private final @Getter String token;
// 
// 	@SneakyThrows
// 	public String getDn() {
// 		var session = DarwinoApplication.get().getLocalJsonDBServer().createSystemSession(null);
// 		try {
// 			var database = session.getDatabase(AppDatabaseDef.DATABASE_NAME);
// 			var store = database.getStore(AppDatabaseDef.STORE_TOKENS);
// 			var query = JsonObject.of("token", token); //$NON-NLS-1$
// 			var tokenDoc = store.openCursor()
// 				.query(query)
// 				.findOneDocument();
// 			return tokenDoc == null ? null : tokenDoc.getString("userName"); //$NON-NLS-1$
// 		} finally {
// 			session.close();
// 		}
// 	}
// 
// 	@Override
// 	public boolean isValid() {
// 		return StringUtil.isNotEmpty(getDn());
// 	}
// 
// 	@Override
// 	public Map<String, String> getAuthenticationHeaders() {
// 		if(isValid()) {
// 			return Collections.singletonMap(HttpBase.HEADER_AUTHORIZATION, "Bearer " + token); //$NON-NLS-1$
// 		}
// 		return null;
// 	}
// }


// Path: frostillicus-blog/frostillicus-blog-j2ee/src/main/java/security/AccessTokenIdentityStore.java
import javax.enterprise.context.ApplicationScoped;
import javax.security.enterprise.credential.Credential;
import javax.security.enterprise.credential.UsernamePasswordCredential;
import javax.security.enterprise.identitystore.CredentialValidationResult;
import javax.security.enterprise.identitystore.IdentityStore;
import javax.security.enterprise.identitystore.IdentityStoreHandler;
import com.darwino.commons.Platform;
import com.darwino.commons.security.acl.UserException;
import com.darwino.commons.security.acl.UserService;
import security.AccessTokenAuthHandler.AccessTokenAuthenticator;

/*
 * Copyright © 2012-2020 Jesse Gallagher
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package security;




/**
 * Implements the Jakarta EE security API for {@link AccessTokenAuthHandler} use.
 *
 * @author Jesse Gallagher
 * @since 2.3.0
 */
@ApplicationScoped
public class AccessTokenIdentityStore implements IdentityStore, IdentityStoreHandler {
	public static final String STORE_ID = AccessTokenIdentityStore.class.getName();

	public CredentialValidationResult validate(final AccessTokenAuthenticator credential) {