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 */
018package org.apache.hadoop.hbase.security.provider.example;
019
020import static java.util.Objects.requireNonNull;
021
022import java.io.DataInput;
023import java.io.DataOutput;
024import java.io.IOException;
025
026import org.apache.hadoop.io.Text;
027import org.apache.hadoop.security.UserGroupInformation;
028import org.apache.hadoop.security.token.TokenIdentifier;
029import org.apache.yetus.audience.InterfaceAudience;
030
031@InterfaceAudience.Private
032public class ShadeTokenIdentifier extends TokenIdentifier {
033  private static final Text TEXT_TOKEN_KIND = new Text(ShadeSaslAuthenticationProvider.TOKEN_KIND);
034  private String username;
035
036  public ShadeTokenIdentifier() {
037    // for ServiceLoader
038  }
039
040  public ShadeTokenIdentifier(String username) {
041    this.username = requireNonNull(username);
042  }
043
044  @Override
045  public void write(DataOutput out) throws IOException {
046    out.writeUTF(username);
047  }
048
049  @Override
050  public void readFields(DataInput in) throws IOException {
051    username = in.readUTF();
052  }
053
054  @Override
055  public Text getKind() {
056    return TEXT_TOKEN_KIND;
057  }
058
059  @Override
060  public UserGroupInformation getUser() {
061    if (username == null || "".equals(username)) {
062      return null;
063    }
064    return UserGroupInformation.createRemoteUser(username);
065  }
066}