001package org.apache.archiva.redback.rest.api.model.v2;
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.keys.AuthenticationKey;
023
024import javax.xml.bind.annotation.XmlRootElement;
025import java.time.Instant;
026import java.time.OffsetDateTime;
027import java.time.ZoneId;
028import java.util.Date;
029
030/**
031 * Represents a authentication token.
032 * @author Martin Stockhammer <martin_s@apache.org>
033 * @since 3.0
034 */
035@XmlRootElement( name = "token" )
036public class Token
037{
038    String key;
039    OffsetDateTime created;
040    OffsetDateTime expires;
041    String principal;
042    String purpose;
043
044    public Token( )
045    {
046    }
047
048    public static Token of( AuthenticationKey key ) {
049        Token token = new Token( );
050        token.setKey( key.getKey() );
051        token.setCreatedFromInstant( key.getDateCreated().toInstant() );
052        token.setExpiresFromInstant( key.getDateExpires().toInstant() );
053        token.setPrincipal( key.getForPrincipal() );
054        token.setPurpose( key.getPurpose() );
055        return token;
056    }
057
058    public static Token of( String key, Date created, Date expires, String principal, String purpose)
059    {
060        Token token = new Token( );
061        token.setKey( key );
062        token.setCreatedFromInstant( created.toInstant( ) );
063        token.setExpiresFromInstant( expires.toInstant( ) );
064        token.setPrincipal( principal );
065        token.setPrincipal( purpose );
066        return token;
067    }
068
069    public static Token of( String key, Instant created, Instant expires, String principal, String purpose )
070    {
071        Token token = new Token( );
072        token.setKey( key );
073        token.setCreatedFromInstant( created );
074        token.setExpiresFromInstant( expires );
075        token.setPrincipal( principal );
076        token.setPrincipal( purpose );
077        return token;
078    }
079
080    @Schema(description = "The key stored in this token.")
081    public String getKey( )
082    {
083        return key;
084    }
085
086    public void setKey( String key )
087    {
088        this.key = key;
089    }
090
091    @Schema(description = "Time, when the token was created")
092    public OffsetDateTime getCreated( )
093    {
094        return created;
095    }
096
097    public void setCreatedFromInstant( Instant created )
098    {
099        this.created = OffsetDateTime.ofInstant( created, ZoneId.of( "UTC" ) );
100    }
101
102    public void setCreated( OffsetDateTime created )
103    {
104        this.created = created;
105    }
106
107    @Schema(description = "Time, when the token expires.")
108    public OffsetDateTime getExpires( )
109    {
110        return expires;
111    }
112
113    public void setExpiresFromInstant( Instant expires )
114    {
115        this.expires = OffsetDateTime.ofInstant( expires, ZoneId.of( "UTC" ) );
116    }
117
118    public void setExpires( OffsetDateTime expires )
119    {
120        this.expires = expires;
121    }
122
123    @Schema(description = "The principal, this token identifies")
124    public String getPrincipal( )
125    {
126        return principal;
127    }
128
129    public void setPrincipal( String principal )
130    {
131        this.principal = principal;
132    }
133
134    @Schema(description = "The purpose of this token")
135    public String getPurpose( )
136    {
137        return purpose;
138    }
139
140    public void setPurpose( String purpose )
141    {
142        this.purpose = purpose;
143    }
144}