001package org.apache.archiva.redback.rest.api.model;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020
021import io.swagger.v3.oas.annotations.media.Schema;
022import org.apache.archiva.redback.authentication.Token;
023
024import javax.xml.bind.annotation.XmlElement;
025import javax.xml.bind.annotation.XmlRootElement;
026import java.io.Serializable;
027import java.time.Duration;
028import java.time.Instant;
029
030/**
031 * @author Martin Stockhammer <martin_s@apache.org>
032 */
033@XmlRootElement(name="token")
034@Schema(name="TokenData", description = "The token response data")
035public class TokenResponse implements Serializable
036{
037
038    private static final long serialVersionUID = 2063260311211245209L;
039    String accessToken;
040    String tokenType = "Bearer";
041    long expiresIn;
042    String refreshToken;
043    String scope;
044    String state;
045
046    public TokenResponse( )
047    {
048    }
049
050    public TokenResponse( String accessToken, String tokenType, long expiresIn, String refreshToken, String scope )
051    {
052        this.accessToken = accessToken;
053        this.tokenType = tokenType;
054        this.expiresIn = expiresIn;
055        this.refreshToken = refreshToken;
056        this.scope = scope;
057    }
058
059    public TokenResponse( String accessToken, long expiresIn, String refreshToken, String scope )
060    {
061        this.accessToken = accessToken;
062        this.expiresIn = expiresIn;
063        this.refreshToken = refreshToken;
064        this.scope = scope;
065    }
066
067    public TokenResponse( Token accessToken, Token refreshToken )
068    {
069        this.expiresIn = Duration.between( Instant.now( ), accessToken.getMetadata( ).validBefore( ) ).getSeconds();
070        this.accessToken = accessToken.getData( );
071        this.refreshToken = refreshToken.getData( );
072        this.scope = "";
073    }
074
075    public TokenResponse( Token accessToken, Token refreshToken , String scope, String state)
076    {
077        this.expiresIn = Duration.between( Instant.now( ), accessToken.getMetadata( ).validBefore( ) ).getSeconds();
078        this.accessToken = accessToken.getData( );
079        this.refreshToken = refreshToken.getData( );
080        this.scope = scope;
081        this.state = state;
082    }
083
084    @XmlElement(name="access_token")
085    @Schema(description = "The access token that may be used as Bearer token in the Authorization header")
086    public String getAccessToken( )
087    {
088        return accessToken;
089    }
090
091    public void setAccessToken( String accessToken )
092    {
093        this.accessToken = accessToken;
094    }
095
096    @XmlElement(name="token_type")
097    @Schema(description = "The type of the token. Currently only Bearer Tokens are supported.")
098    public String getTokenType( )
099    {
100        return tokenType;
101    }
102
103    public void setTokenType( String tokenType )
104    {
105        this.tokenType = tokenType;
106    }
107
108    @XmlElement(name="expires_in")
109    @Schema(description = "The time in seconds. After this time the token will expire and is not valid for authentication.")
110    public long getExpiresIn( )
111    {
112        return expiresIn;
113    }
114
115    public void setExpiresIn( long expiresIn )
116    {
117        this.expiresIn = expiresIn;
118    }
119
120    @XmlElement(name="refresh_token")
121    @Schema(description = "The refresh token, that can be used for getting a new access token.")
122    public String getRefreshToken( )
123    {
124        return refreshToken;
125    }
126
127    public void setRefreshToken( String refreshToken )
128    {
129        this.refreshToken = refreshToken;
130    }
131
132    @Schema(description = "Scope of the token. Currently there are no scopes defined.")
133    public String getScope( )
134    {
135        return scope;
136    }
137
138    public void setScope( String scope )
139    {
140        this.scope = scope;
141    }
142
143    @Schema(description = "The state value will be returned, if a state is provided in the request.")
144    public String getState( )
145    {
146        return state;
147    }
148
149    public void setState( String state )
150    {
151        this.state = state;
152    }
153
154    public boolean hasState() {
155        return state != null && state.length( ) > 0;
156    }
157}