001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.hadoop.security.token; 020 021import java.io.IOException; 022import java.util.Arrays; 023 024import org.apache.commons.codec.digest.DigestUtils; 025import org.apache.hadoop.classification.InterfaceAudience; 026import org.apache.hadoop.classification.InterfaceStability; 027import org.apache.hadoop.io.DataOutputBuffer; 028import org.apache.hadoop.io.Text; 029import org.apache.hadoop.io.Writable; 030import org.apache.hadoop.security.UserGroupInformation; 031 032/** 033 * An identifier that identifies a token, may contain public information 034 * about a token, including its kind (or type). 035 */ 036@InterfaceAudience.Public 037@InterfaceStability.Evolving 038public abstract class TokenIdentifier implements Writable { 039 040 private String trackingId = null; 041 042 /** 043 * Get the token kind 044 * @return the kind of the token 045 */ 046 public abstract Text getKind(); 047 048 /** 049 * Get the Ugi with the username encoded in the token identifier 050 * 051 * @return the username. null is returned if username in the identifier is 052 * empty or null. 053 */ 054 public abstract UserGroupInformation getUser(); 055 056 /** 057 * Get the bytes for the token identifier 058 * @return the bytes of the identifier 059 */ 060 public byte[] getBytes() { 061 DataOutputBuffer buf = new DataOutputBuffer(4096); 062 try { 063 this.write(buf); 064 } catch (IOException ie) { 065 throw new RuntimeException("i/o error in getBytes", ie); 066 } 067 return Arrays.copyOf(buf.getData(), buf.getLength()); 068 } 069 070 /** 071 * Returns a tracking identifier that can be used to associate usages of a 072 * token across multiple client sessions. 073 * 074 * Currently, this function just returns an MD5 of {{@link #getBytes()}. 075 * 076 * @return tracking identifier 077 */ 078 public String getTrackingId() { 079 if (trackingId == null) { 080 trackingId = DigestUtils.md5Hex(getBytes()); 081 } 082 return trackingId; 083 } 084}