View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.security.token;
20  
21  import javax.crypto.SecretKey;
22  
23  import java.io.DataInput;
24  import java.io.DataOutput;
25  import java.io.IOException;
26  import java.util.Arrays;
27  
28  import org.apache.hadoop.hbase.util.Bytes;
29  import org.apache.hadoop.io.Writable;
30  import org.apache.hadoop.io.WritableUtils;
31  
32  /**
33   * Represents a secret key used for signing and verifying authentication tokens
34   * by {@link AuthenticationTokenSecretManager}.
35   */
36  public class AuthenticationKey implements Writable {
37    private int id;
38    private long expirationDate;
39    private SecretKey secret;
40  
41    public AuthenticationKey() {
42      // for Writable
43    }
44  
45    public AuthenticationKey(int keyId, long expirationDate, SecretKey key) {
46      this.id = keyId;
47      this.expirationDate = expirationDate;
48      this.secret = key;
49    }
50  
51    public int getKeyId() {
52      return id;
53    }
54  
55    public long getExpiration() {
56      return expirationDate;
57    }
58  
59    public void setExpiration(long timestamp) {
60      expirationDate = timestamp;
61    }
62  
63    SecretKey getKey() {
64      return secret;
65    }
66  
67    @Override
68    public int hashCode() {
69      int result = id;
70      result = 31 * result + (int) (expirationDate ^ (expirationDate >>> 32));
71      result = 31 * result + ((secret == null) ? 0 : Arrays.hashCode(secret.getEncoded()));
72      return result;
73    }
74  
75    @Override
76    public boolean equals(Object obj) {
77      if (obj == null || !(obj instanceof AuthenticationKey)) {
78        return false;
79      }
80      AuthenticationKey other = (AuthenticationKey)obj;
81      return id == other.getKeyId() &&
82          expirationDate == other.getExpiration() &&
83          (secret == null ? other.getKey() == null :
84              other.getKey() != null &&
85                  Bytes.equals(secret.getEncoded(), other.getKey().getEncoded()));       
86    }
87  
88    @Override
89    public String toString() {
90      StringBuilder buf = new StringBuilder();
91      buf.append("AuthenticationKey[ ")
92         .append("id=").append(id)
93         .append(", expiration=").append(expirationDate)
94         .append(" ]");
95      return buf.toString();
96    }
97  
98    @Override
99    public void write(DataOutput out) throws IOException {
100     WritableUtils.writeVInt(out, id);
101     WritableUtils.writeVLong(out, expirationDate);
102     if (secret == null) {
103       WritableUtils.writeVInt(out, -1);
104     } else {
105       byte[] keyBytes = secret.getEncoded();
106       WritableUtils.writeVInt(out, keyBytes.length);
107       out.write(keyBytes);
108     }
109   }
110 
111   @Override
112   public void readFields(DataInput in) throws IOException {
113     id = WritableUtils.readVInt(in);
114     expirationDate = WritableUtils.readVLong(in);
115     int keyLength = WritableUtils.readVInt(in);
116     if (keyLength < 0) {
117       secret = null;
118     } else {
119       byte[] keyBytes = new byte[keyLength];
120       in.readFully(keyBytes);
121       secret = AuthenticationTokenSecretManager.createSecretKey(keyBytes);
122     }
123   }
124 }