Skip to content

Commit 03a26bf

Browse files
authored
Improve Python token propagation examples (#1873)
1 parent 855ad2f commit 03a26bf

11 files changed

Lines changed: 25 additions & 32 deletions

File tree

‎guides/micronaut-token-propagation/gateway/python/src/example/micronaut/user_controller.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from micronaut.http.annotation import Controller, Get, Produces
33
from micronaut.security.annotation import Secured
44
from micronaut.security.rules import SecurityRule
5-
from org.reactivestreams import Publisher
65

76
from .username_fetcher import UsernameFetcher
87

@@ -16,6 +15,6 @@ def __init__(self, username_fetcher: UsernameFetcher):
1615
@Secured(SecurityRule.IS_AUTHENTICATED)
1716
@Produces(MediaType.TEXT_PLAIN)
1817
@Get
19-
def index(self) -> Publisher[str]:
20-
return self.username_fetcher.findUsername()
18+
async def index(self) -> str:
19+
return await self.username_fetcher.find_username()
2120
# end::clazz[]

‎guides/micronaut-token-propagation/gateway/python/src/example/micronaut/user_echo_client.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from micronaut.http import MediaType
66
from micronaut.http.annotation import Consumes, Get
77
from micronaut.http.client.annotation import Client
8-
from org.reactivestreams import Publisher
98

109
from .username_fetcher import UsernameFetcher
1110

@@ -18,6 +17,6 @@ class UserEchoClient(UsernameFetcher):
1817
@Consumes(MediaType.TEXT_PLAIN)
1918
@Get("/user")
2019
@abstractmethod
21-
def findUsername(self) -> Publisher[str]:
20+
async def find_username(self) -> str:
2221
...
2322
# end::clazz[]
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from abc import ABC, abstractmethod
22

3-
from org.reactivestreams import Publisher
4-
53

64
# tag::clazz[]
75
class UsernameFetcher(ABC):
86
@abstractmethod
9-
def findUsername(self) -> Publisher[str]:
7+
async def find_username(self) -> str:
108
...
119
# end::clazz[]
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from jakarta.inject import Singleton
22
from micronaut.context.annotation import Requires
3-
from org.reactivestreams import Publisher
4-
from reactor.core.publisher import Mono
53

64
from example.micronaut.username_fetcher import UsernameFetcher
75

@@ -11,6 +9,6 @@
119
@Singleton
1210
class UserEchoClientReplacement(UsernameFetcher):
1311

14-
def findUsername(self) -> Publisher[str]:
15-
return Mono.just("sherlock")
12+
async def find_username(self) -> str:
13+
return "sherlock"
1614
# end::clazz[]

‎guides/micronaut-token-propagation/intermediate-gateway/python/src/example/micronaut/user_controller.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from micronaut.http.annotation import Controller, Get, Header, Produces
55
from micronaut.security.annotation import Secured
66
from micronaut.security.rules import SecurityRule
7-
from org.reactivestreams import Publisher
87

98
from .username_fetcher import UsernameFetcher
109

@@ -18,9 +17,9 @@ def __init__(self, username_fetcher: UsernameFetcher): # <2>
1817
@Secured(SecurityRule.IS_AUTHENTICATED) # <3>
1918
@Produces(MediaType.TEXT_PLAIN) # <4>
2019
@Get # <5>
21-
def index(
20+
async def index(
2221
self,
2322
authorization: Annotated[str, Header(HttpHeaders.AUTHORIZATION)], # <6>
24-
) -> Publisher[str]:
25-
return self.username_fetcher.findUsername(authorization)
23+
) -> str:
24+
return await self.username_fetcher.find_username(authorization)
2625
# end::clazz[]

‎guides/micronaut-token-propagation/intermediate-gateway/python/src/example/micronaut/user_echo_client.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from micronaut.http import HttpHeaders, MediaType
77
from micronaut.http.annotation import Consumes, Get, Header
88
from micronaut.http.client.annotation import Client
9-
from org.reactivestreams import Publisher
109

1110
from .username_fetcher import UsernameFetcher
1211

@@ -19,9 +18,9 @@ class UserEchoClient(UsernameFetcher):
1918
@Consumes(MediaType.TEXT_PLAIN)
2019
@Get("/user") # <3>
2120
@abstractmethod
22-
def findUsername(
21+
async def find_username(
2322
self,
2423
authorization: Annotated[str, Header(HttpHeaders.AUTHORIZATION)], # <4>
25-
) -> Publisher[str]:
24+
) -> str:
2625
...
2726
# end::clazz[]
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from abc import ABC, abstractmethod
22

3-
from org.reactivestreams import Publisher
4-
53

64
# tag::clazz[]
75
class UsernameFetcher(ABC):
86
@abstractmethod
9-
def findUsername(self, authorization: str) -> Publisher[str]:
7+
async def find_username(self, authorization: str) -> str:
108
...
119
# end::clazz[]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from jakarta.inject import Singleton
22
from micronaut.context.annotation import Requires
3-
from org.reactivestreams import Publisher
4-
from reactor.core.publisher import Mono
53

64
from example.micronaut.username_fetcher import UsernameFetcher
75

@@ -11,6 +9,6 @@
119
@Singleton
1210
class UserEchoClientReplacement(UsernameFetcher):
1311

14-
def findUsername(self, authorization: str) -> Publisher[str]:
15-
return Mono.just("sherlock")
12+
async def find_username(self, authorization: str) -> str:
13+
return "sherlock"
1614
# end::clazz[]
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
from java.security import Principal
21
from micronaut.http import MediaType
32
from micronaut.http.annotation import Get, Produces
43
from micronaut.security.annotation import Secured
4+
from micronaut.security.authentication import Authentication
55
from micronaut.security.rules import SecurityRule
66

77

88
# tag::clazz[]
99
@Secured(SecurityRule.IS_AUTHENTICATED) # <2>
1010
@Produces(MediaType.TEXT_PLAIN) # <3>
1111
@Get("/user") # <1> <4>
12-
def index(principal: Principal) -> str: # <5>
13-
return principal.getName()
12+
def index(authentication: Authentication) -> str: # <5>
13+
return authentication.getName()
1414
# end::clazz[]

‎guides/micronaut-token-propagation/micronaut-token-propagation.adoc‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ source:UserController[app=intermediate-userecho]
155155
<2> Annotate with `io.micronaut.security.Secured` to configure secured access. The `isAuthenticated()` expression will allow access only to authenticated users.
156156
<3> Since we return a string which is not valid JSON, set the media type to `text/plain`.
157157
<4> You can specify the HTTP verb that a controller action responds to. To respond to a GET request, use the `io.micronaut.http.annotation.Get` annotation.
158+
:exclude-for-languages:python
158159
<5> If a user is authenticated, the Micronaut framework will bind the user object to an argument of type `java.security.Principal` (if present).
160+
:exclude-for-languages:
161+
:only-for-languages:python
162+
<5> If a user is authenticated, the Micronaut framework binds the `Authentication` object to a controller argument of that type.
163+
:only-for-languages:
159164

160165
Add this snippet to the application configuration to change the port where `userecho` starts:
161166

0 commit comments

Comments
 (0)