|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.calcite.avatica.remote; |
| 18 | + |
| 19 | +import org.apache.hc.client5.http.auth.AuthChallenge; |
| 20 | +import org.apache.hc.client5.http.auth.AuthScheme; |
| 21 | +import org.apache.hc.client5.http.auth.AuthScope; |
| 22 | +import org.apache.hc.client5.http.auth.AuthenticationException; |
| 23 | +import org.apache.hc.client5.http.auth.Credentials; |
| 24 | +import org.apache.hc.client5.http.auth.CredentialsProvider; |
| 25 | +import org.apache.hc.client5.http.auth.MalformedChallengeException; |
| 26 | +import org.apache.hc.core5.http.HttpHost; |
| 27 | +import org.apache.hc.core5.http.HttpRequest; |
| 28 | +import org.apache.hc.core5.http.NameValuePair; |
| 29 | +import org.apache.hc.core5.http.protocol.HttpContext; |
| 30 | +import org.apache.hc.core5.util.Args; |
| 31 | +import org.apache.hc.core5.util.Asserts; |
| 32 | + |
| 33 | +import org.slf4j.Logger; |
| 34 | +import org.slf4j.LoggerFactory; |
| 35 | + |
| 36 | +import java.io.Serializable; |
| 37 | +import java.security.Principal; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Locale; |
| 41 | +import java.util.Map; |
| 42 | + |
| 43 | +public class BearerScheme implements AuthScheme, Serializable { |
| 44 | + private static final Logger LOG = LoggerFactory.getLogger(BearerScheme.class); |
| 45 | + private String token; |
| 46 | + |
| 47 | + private final Map<String, String> paramMap; |
| 48 | + private boolean complete; |
| 49 | + |
| 50 | + public BearerScheme() { |
| 51 | + super(); |
| 52 | + this.paramMap = new HashMap<>(); |
| 53 | + this.complete = false; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public String getName() { |
| 58 | + return "Bearer"; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean isConnectionBased() { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public String getRealm() { |
| 68 | + return this.paramMap.get("realm"); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void processChallenge( |
| 73 | + final AuthChallenge authChallenge, |
| 74 | + final HttpContext context) throws MalformedChallengeException { |
| 75 | + this.paramMap.clear(); |
| 76 | + final List<NameValuePair> params = authChallenge.getParams(); |
| 77 | + if (params != null) { |
| 78 | + for (final NameValuePair param: params) { |
| 79 | + this.paramMap.put(param.getName().toLowerCase(Locale.ROOT), param.getValue()); |
| 80 | + } |
| 81 | + if (LOG.isDebugEnabled()) { |
| 82 | + final String error = paramMap.get("error"); |
| 83 | + if (error != null) { |
| 84 | + final StringBuilder buf = new StringBuilder(); |
| 85 | + buf.append(error); |
| 86 | + final String desc = paramMap.get("error_description"); |
| 87 | + final String uri = paramMap.get("error_uri"); |
| 88 | + if (desc != null || uri != null) { |
| 89 | + buf.append(" ("); |
| 90 | + buf.append(desc).append("; ").append(uri); |
| 91 | + buf.append(")"); |
| 92 | + } |
| 93 | + LOG.debug(buf.toString()); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + this.complete = true; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public boolean isChallengeComplete() { |
| 102 | + return this.complete; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public boolean isResponseReady( |
| 107 | + final HttpHost host, |
| 108 | + final CredentialsProvider credentialsProvider, |
| 109 | + final HttpContext context) throws AuthenticationException { |
| 110 | + Args.notNull(host, "Auth host"); |
| 111 | + Args.notNull(credentialsProvider, "CredentialsProvider"); |
| 112 | + |
| 113 | + final Credentials credentials = credentialsProvider.getCredentials( |
| 114 | + new AuthScope(host, null, getName()), context); |
| 115 | + |
| 116 | + if (!(credentials instanceof BearerCredentials)) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + this.token = ((BearerCredentials) credentials).getToken(); |
| 121 | + return null != this.token; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Principal getPrincipal() { |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public String generateAuthResponse( |
| 131 | + final HttpHost host, |
| 132 | + final HttpRequest request, |
| 133 | + final HttpContext context) throws AuthenticationException { |
| 134 | + Asserts.notNull(this.token, "Bearer token"); |
| 135 | + return "Bearer " + this.token; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public String toString() { |
| 140 | + return getName() + this.paramMap; |
| 141 | + } |
| 142 | +} |
0 commit comments