001/*
002 * Copyright (c) 2011-2017 Nexmo Inc
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.nexmo.client.auth;
023
024import com.fasterxml.jackson.databind.ObjectMapper;
025import com.fasterxml.jackson.databind.node.ObjectNode;
026import com.nexmo.client.NexmoUnexpectedException;
027import org.apache.commons.codec.binary.Base64;
028import org.apache.http.Header;
029import org.apache.http.HttpEntity;
030import org.apache.http.client.methods.RequestBuilder;
031import org.apache.http.entity.ContentType;
032import org.apache.http.entity.StringEntity;
033import org.apache.http.message.BasicHeader;
034import org.apache.http.util.EntityUtils;
035
036import java.io.IOException;
037
038public class TokenAuthMethod extends AbstractAuthMethod {
039    private final int SORT_KEY = 30;
040
041    private String apiKey;
042    private String apiSecret;
043
044    public TokenAuthMethod(String apiKey, String apiSecret) {
045        this.apiKey = apiKey;
046        this.apiSecret = apiSecret;
047    }
048
049    @Override
050    public RequestBuilder apply(RequestBuilder request) {
051        return request.addParameter("api_key", this.apiKey).addParameter("api_secret", this.apiSecret);
052    }
053
054    @Override
055    public RequestBuilder applyAsBasicAuth(RequestBuilder request) {
056        String headerValue = Base64.encodeBase64String((this.apiKey + ":" + this.apiSecret).getBytes());
057        Header authHeader = new BasicHeader("Authorization", "Basic " + headerValue);
058        return request.addHeader(authHeader);
059    }
060
061    @Override
062    public RequestBuilder applyAsJsonProperties(RequestBuilder request) {
063        HttpEntity entity = request.getEntity();
064        try {
065            ObjectNode json = (ObjectNode) new ObjectMapper().readTree(EntityUtils.toString(entity));
066            json.put("api_key", this.apiKey);
067            json.put("api_secret", this.apiSecret);
068
069            return request.setEntity(new StringEntity(json.toString(), ContentType.APPLICATION_JSON));
070        } catch (IOException e) {
071            throw new NexmoUnexpectedException("Failed to attach api key and secret to json.", e);
072        }
073    }
074
075    @Override
076    public int getSortKey() {
077        return SORT_KEY;
078    }
079}