PageRenderTime 223ms CodeModel.GetById 100ms app.highlight 46ms RepoModel.GetById 74ms app.codeStats 1ms

/src/main/java/org/expressme/openid/Association.java

http://jopenid.googlecode.com/
Java | 105 lines | 44 code | 17 blank | 44 comment | 0 complexity | 452b2c0ded897b2361b3924e26e968cd MD5 | raw file
  1package org.expressme.openid;
  2
  3import java.text.SimpleDateFormat;
  4
  5/**
  6 * Assocation between RP and OP, and will be cached in memory for a certain time.
  7 * 
  8 * @author Michael Liao (askxuefeng@gmail.com)
  9 */
 10public class Association {
 11
 12    /**
 13     * Session type constant "no-encryption".
 14     */
 15    public static final String SESSION_TYPE_NO_ENCRYPTION = "no-encryption";
 16
 17    /**
 18     * Association type constant "HMAC-SHA1".
 19     */
 20    public static final String ASSOC_TYPE_HMAC_SHA1 = "HMAC-SHA1";
 21
 22    private String session_type;
 23    private String assoc_type;
 24    private String assoc_handle;
 25    private String mac_key;
 26    private byte[] raw_mac_key;
 27    private long expired;
 28
 29    /**
 30     * Get session type.
 31     */
 32    public String getSessionType() { return session_type; }
 33
 34    /**
 35     * Set session type.
 36     */
 37    public void setSessionType(String session_type) { this.session_type = session_type; }
 38
 39    /**
 40     * Get association type.
 41     */
 42    public String getAssociationType() { return assoc_type; }
 43
 44    /**
 45     * Set association type.
 46     */
 47    public void setAssociationType(String assoc_type) { this.assoc_type = assoc_type; }
 48
 49    /**
 50     * Get association handle.
 51     */
 52    public String getAssociationHandle() { return assoc_handle; }
 53
 54    /**
 55     * Set association handle.
 56     */
 57    public void setAssociationHandle(String assoc_handle) { this.assoc_handle = assoc_handle; }
 58
 59    /**
 60     * Get MAC key.
 61     */
 62    public String getMacKey() { return mac_key; }
 63
 64    /**
 65     * Set MAC key.
 66     */
 67    public void setMacKey(String mac_key) {
 68        this.mac_key = mac_key;
 69        this.raw_mac_key = Base64.decode(mac_key);
 70    }
 71
 72    /**
 73     * Get raw MAC key as bytes.
 74     */
 75    public byte[] getRawMacKey() {
 76        return raw_mac_key;
 77    }
 78
 79    /**
 80     * Set max age in milliseconds.
 81     */
 82    public void setMaxAge(long maxAgeInMilliseconds) {
 83        this.expired = System.currentTimeMillis() + maxAgeInMilliseconds;
 84    }
 85
 86    /**
 87     * Detect if this association is expired.
 88     */
 89    public boolean isExpired() {
 90        return System.currentTimeMillis() >= expired;
 91    }
 92
 93    @Override
 94    public String toString() {
 95        StringBuilder sb = new StringBuilder(1024);
 96        sb.append("Association [")
 97          .append("session_type:").append(session_type).append(", ")
 98          .append("assoc_type:").append(assoc_type).append(", ")
 99          .append("assoc_handle:").append(assoc_handle).append(", ")
100          .append("mac_key:").append(mac_key).append(", ")
101          .append("expired:").append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(expired))
102          .append(']');
103        return sb.toString();
104    }
105}