-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a token provider and custom scopes example
- Loading branch information
Showing
4 changed files
with
66 additions
and
0 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
20 changes: 20 additions & 0 deletions
20
examples/src/main/java/nakadi/examples/oauth/MyTokenProvider.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,20 @@ | ||
package nakadi.examples.oauth; | ||
|
||
import java.util.Optional; | ||
import nakadi.TokenProvider; | ||
|
||
public class MyTokenProvider implements TokenProvider { | ||
|
||
/** | ||
* @return a value suitable for use in an Authorization header, or null to suppress the | ||
* Authorization header being set | ||
*/ | ||
@Override public Optional<String> authHeaderValue(String scope) { | ||
|
||
if("gordian-blade-scope".equals(scope)) { | ||
return Optional.of("code-gate-token"); | ||
} | ||
|
||
return Optional.of("icebreaker-uid-token"); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
examples/src/main/java/nakadi/examples/oauth/OAuthScopesMain.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,41 @@ | ||
package nakadi.examples.oauth; | ||
|
||
import nakadi.BusinessEventMapped; | ||
import nakadi.EventMetadata; | ||
import nakadi.EventResource; | ||
import nakadi.NakadiClient; | ||
import nakadi.examples.events.PriorityRequisition; | ||
|
||
public class OAuthScopesMain { | ||
|
||
public static void main(String[] args) { | ||
|
||
String baseURI = "http://localhost:" + 9080; | ||
|
||
NakadiClient client = NakadiClient.newBuilder() | ||
.baseURI(baseURI) | ||
/* | ||
Configure the client with a token provider. All scopes are sent to this TokenProvider | ||
per request to be resolved to tokens. Because it's per request, providers can refresh | ||
in the background. | ||
*/ | ||
.tokenProvider(new MyTokenProvider()) | ||
.build(); | ||
|
||
BusinessEventMapped<PriorityRequisition> event = new BusinessEventMapped<PriorityRequisition>() | ||
.metadata(new EventMetadata()) | ||
.data(new PriorityRequisition("22")); | ||
|
||
EventResource events = client.resources().events(); | ||
|
||
/* | ||
You can set the oauth scope on most requests using the scope() option. | ||
This allows for custom or tenant level scopes to be used (in the future). | ||
Otherwise the default scopes defined in the Nakadi API definition are used. | ||
*/ | ||
events | ||
.scope("gordian-blade-scope") | ||
.send("priority-requisition-biz", event); | ||
|
||
} | ||
} |