com.fujitsu.dc.common.auth.token.AccountAccessToken.java Source code

Java tutorial

Introduction

Here is the source code for com.fujitsu.dc.common.auth.token.AccountAccessToken.java

Source

/**
 * personium.io
 * Copyright 2014 FUJITSU LIMITED
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fujitsu.dc.common.auth.token;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Account Access Token ???.
 */
public final class AccountAccessToken extends CellLocalAccessToken implements IAccessToken {

    /**
     * .
     */
    static Logger log = LoggerFactory.getLogger(AccountAccessToken.class);

    /**
     * ?PREFIX.
     */
    public static final String PREFIX_ACCESS = "AA~";

    static final int IDX_COUNT = 5;
    static final int IDX_ISSUED_AT = 0;
    static final int IDX_LIFESPAN = 1;
    static final int IDX_ISSUER = 4;
    static final int IDX_SUBJECT = 2;
    static final int IDX_SCHEMA = 3;

    /**
     * ?.
     */
    public static final int ACCESS_TOKEN_EXPIRES_HOUR = 1;

    /**
     * ?????.
     * @param issuedAt (epoch??)
     * @param lifespan ()
     * @param issuer 
     * @param subject Subject
     * @param schema Schema
     */
    public AccountAccessToken(final long issuedAt, final long lifespan, final String issuer, final String subject,
            final String schema) {
        super(issuedAt, lifespan, issuer, subject, null, schema);
    }

    /**
     * ?????.
     * @param issuedAt (epoch??)
     * @param issuer 
     * @param subject Subject
     * @param schema Schema
     */
    public AccountAccessToken(final long issuedAt, final String issuer, final String subject, final String schema) {
        this(issuedAt, ACCESS_TOKEN_EXPIRES_HOUR * MILLISECS_IN_AN_HOUR, issuer, subject, schema);
    }

    @Override
    public String toTokenString() {
        StringBuilder ret = new StringBuilder(PREFIX_ACCESS);
        ret.append(this.doCreateTokenString(null));
        return ret.toString();
    }

    /**
     * issuer???Cell????.
     * @param token Token String
     * @param issuer Cell Root URL
     * @return ??CellLocalToken
     * @throws AbstractOAuth2Token.TokenParseException ????????
     */
    public static AccountAccessToken parse(final String token, final String issuer)
            throws AbstractOAuth2Token.TokenParseException {
        if (!token.startsWith(PREFIX_ACCESS) || issuer == null) {
            throw AbstractOAuth2Token.PARSE_EXCEPTION;
        }
        String[] frag = LocalToken.doParse(token.substring(PREFIX_ACCESS.length()), issuer, IDX_COUNT);

        try {
            AccountAccessToken ret = new AccountAccessToken(Long.valueOf(StringUtils.reverse(frag[IDX_ISSUED_AT])),
                    Long.valueOf(frag[IDX_LIFESPAN]), frag[IDX_ISSUER], frag[IDX_SUBJECT], frag[IDX_SCHEMA]);
            return ret;
        } catch (Exception e) {
            throw AbstractOAuth2Token.PARSE_EXCEPTION;
        }
    }
}