001/* 002 * Copyright 2024 Vonage 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.vonage.client.auth; 017 018import java.util.Base64; 019 020/** 021 * API key and secret in the header. 022 * 023 * @since 8.8.0 024 */ 025public class ApiKeyHeaderAuthMethod extends BasicAuthMethod implements ApiKeyAuthMethod { 026 private static final int SORT_KEY = 40; 027 028 private final String apiKey, apiSecret; 029 030 public ApiKeyHeaderAuthMethod(String apiKey, String apiSecret) { 031 this.apiKey = apiKey; 032 this.apiSecret = apiSecret; 033 } 034 035 @Override 036 public String getApiKey() { 037 return apiKey; 038 } 039 040 /** 041 * Converts this to a {@linkplain QueryParamsAuthMethod}. 042 * 043 * @return A new {@linkplain ApiKeyQueryParamsAuthMethod} with this object's API key and secret. 044 * @deprecated This will be removed in a future release. 045 */ 046 @Deprecated 047 public QueryParamsAuthMethod asQueryParams() { 048 return new ApiKeyQueryParamsAuthMethod(apiKey, apiSecret); 049 } 050 051 @Override 052 protected String getBasicToken() { 053 return Base64.getEncoder().encodeToString((apiKey + ":" + apiSecret).getBytes()); 054 } 055 056 @Override 057 public int getSortKey() { 058 return SORT_KEY; 059 } 060}