-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CALCITE-6135] BEARER authentication support
- Loading branch information
Showing
22 changed files
with
1,481 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ public enum AuthenticationType { | |
BASIC, | ||
DIGEST, | ||
SPNEGO, | ||
BEARER, | ||
CUSTOM; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
core/src/main/java/org/apache/calcite/avatica/remote/BearerAuthenticateable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.calcite.avatica.remote; | ||
|
||
public interface BearerAuthenticateable { | ||
|
||
void setTokenProvider(String username, BearerTokenProvider tokenProvider); | ||
} |
54 changes: 54 additions & 0 deletions
54
core/src/main/java/org/apache/calcite/avatica/remote/BearerCredentials.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.calcite.avatica.remote; | ||
|
||
import org.apache.hc.client5.http.auth.Credentials; | ||
import org.apache.hc.core5.annotation.Contract; | ||
import org.apache.hc.core5.annotation.ThreadingBehavior; | ||
import org.apache.hc.core5.util.Args; | ||
|
||
import java.io.Serializable; | ||
import java.security.Principal; | ||
|
||
@Contract(threading = ThreadingBehavior.IMMUTABLE) | ||
public class BearerCredentials implements Credentials, Serializable { | ||
|
||
private final BearerTokenProvider tokenProvider; | ||
|
||
private final String userName; | ||
|
||
public BearerCredentials(final String userName, final BearerTokenProvider tokenProvider) { | ||
Args.notNull(userName, "userName"); | ||
Args.notNull(tokenProvider, "tokenProvider"); | ||
this.tokenProvider = tokenProvider; | ||
this.userName = userName; | ||
} | ||
|
||
public String getToken() { | ||
return tokenProvider.obtain(userName); | ||
} | ||
|
||
@Override | ||
public Principal getUserPrincipal() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public char[] getPassword() { | ||
return null; | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
core/src/main/java/org/apache/calcite/avatica/remote/BearerScheme.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.calcite.avatica.remote; | ||
|
||
import org.apache.hc.client5.http.auth.AuthChallenge; | ||
import org.apache.hc.client5.http.auth.AuthScheme; | ||
import org.apache.hc.client5.http.auth.AuthScope; | ||
import org.apache.hc.client5.http.auth.AuthenticationException; | ||
import org.apache.hc.client5.http.auth.Credentials; | ||
import org.apache.hc.client5.http.auth.CredentialsProvider; | ||
import org.apache.hc.client5.http.auth.MalformedChallengeException; | ||
import org.apache.hc.core5.http.HttpHost; | ||
import org.apache.hc.core5.http.HttpRequest; | ||
import org.apache.hc.core5.http.NameValuePair; | ||
import org.apache.hc.core5.http.protocol.HttpContext; | ||
import org.apache.hc.core5.util.Args; | ||
import org.apache.hc.core5.util.Asserts; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.Serializable; | ||
import java.security.Principal; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
public class BearerScheme implements AuthScheme, Serializable { | ||
private static final Logger LOG = LoggerFactory.getLogger(BearerScheme.class); | ||
private String token; | ||
|
||
private final Map<String, String> paramMap; | ||
private boolean complete; | ||
|
||
public BearerScheme() { | ||
super(); | ||
this.paramMap = new HashMap<>(); | ||
this.complete = false; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "Bearer"; | ||
} | ||
|
||
@Override | ||
public boolean isConnectionBased() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String getRealm() { | ||
return this.paramMap.get("realm"); | ||
} | ||
|
||
@Override | ||
public void processChallenge( | ||
final AuthChallenge authChallenge, | ||
final HttpContext context) throws MalformedChallengeException { | ||
this.paramMap.clear(); | ||
final List<NameValuePair> params = authChallenge.getParams(); | ||
if (params != null) { | ||
for (final NameValuePair param: params) { | ||
this.paramMap.put(param.getName().toLowerCase(Locale.ROOT), param.getValue()); | ||
} | ||
if (LOG.isDebugEnabled()) { | ||
final String error = paramMap.get("error"); | ||
if (error != null) { | ||
final StringBuilder buf = new StringBuilder(); | ||
buf.append(error); | ||
final String desc = paramMap.get("error_description"); | ||
final String uri = paramMap.get("error_uri"); | ||
if (desc != null || uri != null) { | ||
buf.append(" ("); | ||
buf.append(desc).append("; ").append(uri); | ||
buf.append(")"); | ||
} | ||
LOG.debug(buf.toString()); | ||
} | ||
} | ||
} | ||
this.complete = true; | ||
} | ||
|
||
@Override | ||
public boolean isChallengeComplete() { | ||
return this.complete; | ||
} | ||
|
||
@Override | ||
public boolean isResponseReady( | ||
final HttpHost host, | ||
final CredentialsProvider credentialsProvider, | ||
final HttpContext context) throws AuthenticationException { | ||
Args.notNull(host, "Auth host"); | ||
Args.notNull(credentialsProvider, "CredentialsProvider"); | ||
|
||
final Credentials credentials = credentialsProvider.getCredentials( | ||
new AuthScope(host, null, getName()), context); | ||
|
||
if (!(credentials instanceof BearerCredentials)) { | ||
return false; | ||
} | ||
|
||
this.token = ((BearerCredentials) credentials).getToken(); | ||
return null != this.token; | ||
} | ||
|
||
@Override | ||
public Principal getPrincipal() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String generateAuthResponse( | ||
final HttpHost host, | ||
final HttpRequest request, | ||
final HttpContext context) throws AuthenticationException { | ||
Asserts.notNull(this.token, "Bearer token"); | ||
return "Bearer " + this.token; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getName() + this.paramMap; | ||
} | ||
} |
Oops, something went wrong.