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 java.io.DataInput;
22  import java.io.DataOutput;
23  import java.io.IOException;
24  
25  import org.apache.hadoop.io.Text;
26  import org.apache.hadoop.io.WritableUtils;
27  import org.apache.hadoop.security.UserGroupInformation;
28  import org.apache.hadoop.security.token.TokenIdentifier;
29  
30  /**
31   * Represents the identity information stored in an HBase authentication token.
32   */
33  public class AuthenticationTokenIdentifier extends TokenIdentifier {
34    public static final byte VERSION = 1;
35    public static final Text AUTH_TOKEN_TYPE = new Text("HBASE_AUTH_TOKEN");
36  
37    protected String username;
38    protected int keyId;
39    protected long issueDate;
40    protected long expirationDate;
41    protected long sequenceNumber;
42    
43    public AuthenticationTokenIdentifier() {
44    }
45  
46    public AuthenticationTokenIdentifier(String username) {
47      this.username = username;
48    }
49  
50    public AuthenticationTokenIdentifier(String username, int keyId,
51        long issueDate, long expirationDate) {
52      this.username = username;
53      this.keyId = keyId;
54      this.issueDate = issueDate;
55      this.expirationDate = expirationDate;
56    }
57  
58    @Override
59    public Text getKind() {
60      return AUTH_TOKEN_TYPE;
61    }
62  
63    @Override
64    public UserGroupInformation getUser() {
65      if (username == null || "".equals(username)) {
66        return null;
67      }
68      return UserGroupInformation.createRemoteUser(username);
69    }
70  
71    public String getUsername() {
72      return username;
73    }
74  
75    void setUsername(String name) {
76      this.username = name;
77    }
78  
79    public int getKeyId() {
80      return keyId;
81    }
82  
83    void setKeyId(int id) {
84      this.keyId = id;
85    }
86  
87    public long getIssueDate() {
88      return issueDate;
89    }
90  
91    void setIssueDate(long timestamp) {
92      this.issueDate = timestamp;
93    }
94  
95    public long getExpirationDate() {
96      return expirationDate;
97    }
98  
99    void setExpirationDate(long timestamp) {
100     this.expirationDate = timestamp;
101   }
102 
103   public long getSequenceNumber() {
104     return sequenceNumber;
105   }
106 
107   void setSequenceNumber(long seq) {
108     this.sequenceNumber = seq;
109   }
110 
111   @Override
112   public void write(DataOutput out) throws IOException {
113     out.writeByte(VERSION);
114     WritableUtils.writeString(out, username);
115     WritableUtils.writeVInt(out, keyId);
116     WritableUtils.writeVLong(out, issueDate);
117     WritableUtils.writeVLong(out, expirationDate);
118     WritableUtils.writeVLong(out, sequenceNumber);
119   }
120 
121   @Override
122   public void readFields(DataInput in) throws IOException {
123     byte version = in.readByte();
124     if (version != VERSION) {
125       throw new IOException("Version mismatch in deserialization: " +
126           "expected="+VERSION+", got="+version);
127     }
128     username = WritableUtils.readString(in);
129     keyId = WritableUtils.readVInt(in);
130     issueDate = WritableUtils.readVLong(in);
131     expirationDate = WritableUtils.readVLong(in);
132     sequenceNumber = WritableUtils.readVLong(in);
133   }
134 
135   @Override
136   public boolean equals(Object other) {
137     if (other == null) {
138       return false;
139     }
140     if (other instanceof AuthenticationTokenIdentifier) {
141       AuthenticationTokenIdentifier ident = (AuthenticationTokenIdentifier)other;
142       return sequenceNumber == ident.getSequenceNumber()
143           && keyId == ident.getKeyId()
144           && issueDate == ident.getIssueDate()
145           && expirationDate == ident.getExpirationDate()
146           && (username == null ? ident.getUsername() == null :
147               username.equals(ident.getUsername()));
148     }
149     return false;
150   }
151 
152   @Override
153   public int hashCode() {
154     return (int)sequenceNumber;
155   }
156 }