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