Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
15fc264c
Unverified
Commit
15fc264c
authored
Mar 08, 2022
by
Gautier de Saint Martin Lacaze
Committed by
GitHub
Mar 08, 2022
Browse files
Merge pull request #786 from Tinkoff/feature/refreshable-token-support
Feature: Replace auth token with supplier
parents
8af68995
013436b8
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/GitLabApi.java
View file @
15fc264c
...
@@ -5,6 +5,7 @@ import java.util.List;
...
@@ -5,6 +5,7 @@ import java.util.List;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.Optional
;
import
java.util.WeakHashMap
;
import
java.util.WeakHashMap
;
import
java.util.function.Supplier
;
import
java.util.logging.Level
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
java.util.logging.Logger
;
...
@@ -711,6 +712,14 @@ public class GitLabApi implements AutoCloseable {
...
@@ -711,6 +712,14 @@ public class GitLabApi implements AutoCloseable {
return
(
apiClient
.
getAuthToken
());
return
(
apiClient
.
getAuthToken
());
}
}
/**
* Set auth token supplier for gitlab api client.
* @param authTokenSupplier - supplier which provide actual auth token
*/
public
void
setAuthTokenSupplier
(
Supplier
<
String
>
authTokenSupplier
)
{
apiClient
.
setAuthTokenSupplier
(
authTokenSupplier
);
}
/**
/**
* Get the secret token.
* Get the secret token.
*
*
...
...
src/main/java/org/gitlab4j/api/GitLabApiClient.java
View file @
15fc264c
...
@@ -10,6 +10,7 @@ import java.security.cert.CertificateException;
...
@@ -10,6 +10,7 @@ import java.security.cert.CertificateException;
import
java.security.cert.X509Certificate
;
import
java.security.cert.X509Certificate
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.function.Supplier
;
import
java.util.logging.Level
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
java.util.logging.Logger
;
...
@@ -60,7 +61,7 @@ public class GitLabApiClient implements AutoCloseable {
...
@@ -60,7 +61,7 @@ public class GitLabApiClient implements AutoCloseable {
private
String
baseUrl
;
private
String
baseUrl
;
private
String
hostUrl
;
private
String
hostUrl
;
private
TokenType
tokenType
=
TokenType
.
PRIVATE
;
private
TokenType
tokenType
=
TokenType
.
PRIVATE
;
private
String
authToken
;
private
Supplier
<
String
>
authToken
;
private
String
secretToken
;
private
String
secretToken
;
private
boolean
ignoreCertificateErrors
;
private
boolean
ignoreCertificateErrors
;
private
SSLContext
openSslContext
;
private
SSLContext
openSslContext
;
...
@@ -215,7 +216,7 @@ public class GitLabApiClient implements AutoCloseable {
...
@@ -215,7 +216,7 @@ public class GitLabApiClient implements AutoCloseable {
this
.
hostUrl
+=
apiVersion
.
getApiNamespace
();
this
.
hostUrl
+=
apiVersion
.
getApiNamespace
();
this
.
tokenType
=
tokenType
;
this
.
tokenType
=
tokenType
;
this
.
authToken
=
authToken
;
this
.
authToken
=
()
->
authToken
;
if
(
secretToken
!=
null
)
{
if
(
secretToken
!=
null
)
{
secretToken
=
secretToken
.
trim
();
secretToken
=
secretToken
.
trim
();
...
@@ -293,7 +294,7 @@ public class GitLabApiClient implements AutoCloseable {
...
@@ -293,7 +294,7 @@ public class GitLabApiClient implements AutoCloseable {
* @return the auth token being used by this client
* @return the auth token being used by this client
*/
*/
String
getAuthToken
()
{
String
getAuthToken
()
{
return
(
authToken
);
return
(
authToken
.
get
()
);
}
}
/**
/**
...
@@ -792,7 +793,7 @@ public class GitLabApiClient implements AutoCloseable {
...
@@ -792,7 +793,7 @@ public class GitLabApiClient implements AutoCloseable {
}
}
String
authHeader
=
(
tokenType
==
TokenType
.
OAUTH2_ACCESS
?
AUTHORIZATION_HEADER
:
PRIVATE_TOKEN_HEADER
);
String
authHeader
=
(
tokenType
==
TokenType
.
OAUTH2_ACCESS
?
AUTHORIZATION_HEADER
:
PRIVATE_TOKEN_HEADER
);
String
authValue
=
(
tokenType
==
TokenType
.
OAUTH2_ACCESS
?
"Bearer "
+
authToken
:
authToken
);
String
authValue
=
(
tokenType
==
TokenType
.
OAUTH2_ACCESS
?
"Bearer "
+
authToken
.
get
()
:
authToken
.
get
()
);
Invocation
.
Builder
builder
=
target
.
request
();
Invocation
.
Builder
builder
=
target
.
request
();
if
(
accept
==
null
||
accept
.
trim
().
length
()
==
0
)
{
if
(
accept
==
null
||
accept
.
trim
().
length
()
==
0
)
{
builder
=
builder
.
header
(
authHeader
,
authValue
);
builder
=
builder
.
header
(
authHeader
,
authValue
);
...
@@ -923,4 +924,12 @@ public class GitLabApiClient implements AutoCloseable {
...
@@ -923,4 +924,12 @@ public class GitLabApiClient implements AutoCloseable {
return
(
true
);
return
(
true
);
}
}
/**
* Set auth token supplier for gitlab api client.
* @param authTokenSupplier - supplier which provide actual auth token
*/
public
void
setAuthTokenSupplier
(
Supplier
<
String
>
authTokenSupplier
)
{
this
.
authToken
=
authTokenSupplier
;
}
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment