Java Code Examples for org.openid4java.association.Association#generate()

The following examples show how to use org.openid4java.association.Association#generate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenIdServiceTests.java    From springboot-shiro-cas-mybatis with MIT License 6 votes vote down vote up
@Test
public void verifyExpiredAssociationGetResponse() {
    request.addParameter("openid.assoc_handle", "test");
    openIdService = OpenIdService.createServiceFrom(request, null);
    Association association = null;
    try {
        association = Association.generate(Association.TYPE_HMAC_SHA1, "test", 2);
    } catch (final Exception e) {
        fail("Could not generate association");
    }
    when(sharedAssociations.load("test")).thenReturn(association);
    synchronized (this) {
        try {
            this.wait(3000);
        } catch (final InterruptedException ie) {
            fail("Could not wait long enough to check association expiry date");
        }
    }
    final Response response = this.openIdService.getResponse("test");
    request.removeParameter("openid.assoc_handle");
    assertNotNull(response);

    assertEquals(1, response.getAttributes().size());
    assertEquals("cancel", response.getAttributes().get("openid.mode"));
}
 
Example 2
Source File: InMemoryServerAssociationStore.java    From openid4java with Apache License 2.0 6 votes vote down vote up
public synchronized Association generate(String type, int expiryIn)
        throws AssociationException
{
    removeExpired();

    String handle = _timestamp + "-" + _counter++;

    Association association = Association.generate(type, handle, expiryIn);

    _handleMap.put(handle, association);

    if (DEBUG) _log.debug("Generated association, handle: " + handle +
                          " type: " + type +
                          " expires in: " + expiryIn + " seconds.");

    return association;
}
 
Example 3
Source File: OpenIDServerAssociationStore.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Super will generate the association and it will be persisted by the DAO.
 *
 * @param type     association type defined in the OpenID 2.0
 * @param expiryIn date
 * @return <code>Association</code>
 */
@Override
public Association generate(String type, int expiryIn)
        throws AssociationException {
    String handle = storeId + timestamp + "-" + getCounter();
    final Association association = Association.generate(type, handle, expiryIn);
    cache.addToCache(association);
    // Asynchronous write to database
    Thread thread = new Thread() {
        @Override
        public void run() {
            if(log.isDebugEnabled()) {
                log.debug("Storing association " + association.getHandle() + " in the database.");
            }
            dao.storeAssociation(association);
        }
    };
    thread.start();
    return association;
}
 
Example 4
Source File: OpenIdServiceTests.java    From cas4.0.x-server-wechat with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpiredAssociationGetResponse() {
    request.addParameter("openid.assoc_handle", "test");
    openIdService = OpenIdService.createServiceFrom(request);
    Association association = null;
    try {
        association = Association.generate(Association.TYPE_HMAC_SHA1, "test", 2);
    } catch (final Exception e) {
        fail("Could not generate association");
    }
    when(context.getBean("serverManager")).thenReturn(manager);
    when(context.getBean("centralAuthenticationService")).thenReturn(cas);
    when(sharedAssociations.load("test")).thenReturn(association);
    synchronized (this) {
        try {
            this.wait(3000);
        } catch (final InterruptedException ie) {
            fail("Could not wait long enough to check association expiry date");
        }
    }
    final Response response = this.openIdService.getResponse("test");
    request.removeParameter("openid.assoc_handle");
    assertNotNull(response);

    assertEquals(1, response.getAttributes().size());
    assertEquals("cancel", response.getAttributes().get("openid.mode"));
}
 
Example 5
Source File: CustomInMemoryServerAssociationStore.java    From OpenID-Attacker with GNU General Public License v2.0 5 votes vote down vote up
@Override
public synchronized Association generate(String type, int expiryIn)
  throws AssociationException {
    removeExpired();

    String handle;
    // If this is the first, just use the prefix
    handle = associationPrefix;
    while (_handleMap.containsKey(handle)) {
        // Otherwise, use prefix plus counter
        ++counter;
        handle = associationPrefix + "-" + counter;
    }

    Association association = Association.generate(type, handle, expiryIn);

    _handleMap.put(handle, association);

    if (DEBUG) {
        LOG.debug("Generated association, handle: " + handle
          + " type: " + type
          + " expires in: " + expiryIn + " seconds.");
    }
    removeExpired();

    return association;
}
 
Example 6
Source File: PrivateAssociationReplicationStore.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
public Association generate(String type, int expiryIn) throws AssociationException {
    String handle = storeId + timestamp + "-" + getCounter();
    Association association = Association.generate(type, handle, expiryIn);

    // replicating association using cluster messages
    if(log.isDebugEnabled()) {
        log.debug("Storing association " + association.getHandle() + " in the map.");
    }
    OpenIDAssociationReplicationManager.getPersistenceManager().addAssociation(association);

    return association;
}
 
Example 7
Source File: JdbcServerAssociationStore.java    From openid4java with Apache License 2.0 4 votes vote down vote up
public Association generate(String type, int expiryIn)
        throws AssociationException
{
    cleanupExpired();
    
    String sql = "INSERT INTO " + _tableName +
            " (handle, type, mackey, expdate) VALUES (?,?,?,?)";

    JdbcTemplate jdbcTemplate = getJdbcTemplate();

    int attemptsLeft = 5;

    while (attemptsLeft > 0)
    {
        try
        {
            String handle = Long.toHexString(_random.nextLong());

            Association association =
                    Association.generate(type, handle, expiryIn);

            int cnt = jdbcTemplate.update(sql,
                    new Object[] {
                            association.getHandle(),
                            association.getType(),
                            new String(Base64.encodeBase64(
                                    association.getMacKey().getEncoded())),
                            association.getExpiry()
                    });

            if (cnt == 1)
            {
                if (DEBUG)
                    _log.debug("Generated association, handle: " + handle +
                               " type: " + type +
                               " expires in: " + expiryIn + " seconds.");

                return association;
            }
        }
        catch (DataAccessException e)
        {
            _log.error("Error generating association; attempts left: "
                       + (attemptsLeft-1), e);
        }

        attemptsLeft--;
    }

    throw new AssociationException(
            "JDBCServerAssociationStore: Error generating association.");
}