1 /* 2 * Copyright 2021 Google LLC 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * 15 * * Neither the name of Google LLC nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 package com.google.auth.oauth2; 33 34 import static com.google.common.base.Preconditions.checkNotNull; 35 36 import java.text.DateFormat; 37 import java.text.ParseException; 38 import java.text.SimpleDateFormat; 39 import java.util.Date; 40 import java.util.TimeZone; 41 42 /** Formats dates required for AWS Signature V4 request signing. */ 43 final class AwsDates { 44 private static final String X_AMZ_DATE_FORMAT = "yyyyMMdd'T'HHmmss'Z'"; 45 private static final String HTTP_DATE_FORMAT = "E, dd MMM yyyy HH:mm:ss z"; 46 47 private final String xAmzDate; 48 private final String originalDate; 49 AwsDates(String amzDate)50 private AwsDates(String amzDate) { 51 this.xAmzDate = checkNotNull(amzDate); 52 this.originalDate = amzDate; 53 } 54 AwsDates(String xAmzDate, String originalDate)55 private AwsDates(String xAmzDate, String originalDate) { 56 this.xAmzDate = checkNotNull(xAmzDate); 57 this.originalDate = checkNotNull(originalDate); 58 } 59 60 /** 61 * Returns the original date. This can either be the x-amz-date or a specified date in the format 62 * of E, dd MMM yyyy HH:mm:ss z. 63 */ getOriginalDate()64 String getOriginalDate() { 65 return originalDate; 66 } 67 68 /** Returns the x-amz-date in yyyyMMdd'T'HHmmss'Z' format. */ getXAmzDate()69 String getXAmzDate() { 70 return xAmzDate; 71 } 72 73 /** Returns the x-amz-date in YYYYMMDD format. */ getFormattedDate()74 String getFormattedDate() { 75 return xAmzDate.substring(0, 8); 76 } 77 fromXAmzDate(String xAmzDate)78 static AwsDates fromXAmzDate(String xAmzDate) throws ParseException { 79 // Validate format 80 new SimpleDateFormat(AwsDates.X_AMZ_DATE_FORMAT).parse(xAmzDate); 81 return new AwsDates(xAmzDate); 82 } 83 fromDateHeader(String date)84 static AwsDates fromDateHeader(String date) throws ParseException { 85 DateFormat dateFormat = new SimpleDateFormat(X_AMZ_DATE_FORMAT); 86 dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); 87 88 Date inputDate = new SimpleDateFormat(HTTP_DATE_FORMAT).parse(date); 89 String xAmzDate = dateFormat.format(inputDate); 90 return new AwsDates(xAmzDate, date); 91 } 92 generateXAmzDate()93 static AwsDates generateXAmzDate() { 94 DateFormat dateFormat = new SimpleDateFormat(X_AMZ_DATE_FORMAT); 95 dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); 96 String xAmzDate = dateFormat.format(new Date(System.currentTimeMillis())); 97 return new AwsDates(xAmzDate); 98 } 99 } 100