Skip to content

Commit

Permalink
feat: add refresh_oauth_token() API (#32)
Browse files Browse the repository at this point in the history
* Fix init imports

* Refresh token was appended
request for access_token export to new method

* Codestyle was fixed

* Codestyle was fixed

* Codestyle was fixed

* Codestyle was fixed
New method appended to Readme
  • Loading branch information
Verdgil authored Dec 7, 2022
1 parent 01e161d commit 352d47e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ casdoor-python-sdk support basic user operations, like:
- `get_user(user_id: str)`, get one user by user name.
- `get_users()`, get all users.
- `modify_user(method: str, user: User)/add_user(user: User)/update_user(user: User)/delete_user(user: User)`, write user to database.
- `refresh_token_request(refresh_token: str, scope: str)`, refresh access token
39 changes: 38 additions & 1 deletion src/casdoor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,51 @@ def get_oauth_token(self, code: str) -> str:
:param code: the code that sent from Casdoor using redirect url back to your server.
:return: access_token
"""
r = self.oauth_token_request(code)
access_token = r.json().get("access_token")

return access_token

def oauth_token_request(self, code: str) -> requests.Response:
"""
Request the Casdoor server to get access_token.
:param code: the code that sent from Casdoor using redirect url back to your server.
:return: Response from Casdoor
"""
url = self.endpoint + "/api/login/oauth/access_token"
params = {
"grant_type": self.grant_type,
"client_id": self.client_id,
"client_secret": self.client_secret,
"code": code,
}
r = requests.post(url, params)
return requests.post(url, params)

def refresh_token_request(self, refresh_token: str, scope: str = "") -> requests.Response:
"""
Request the Casdoor server to get access_token.
:param refresh_token: refresh_token for send to Casdoor
:param scope: OAuth scope
:return: Response from Casdoor
"""
url = self.endpoint + "/api/login/oauth/refresh_token"
params = {
"grant_type": self.grant_type,
"client_id": self.client_id,
"client_secret": self.client_secret,
"scope": scope,
"refresh_token": refresh_token,
}
return requests.post(url, params)

def refresh_oauth_token(self, refresh_token: str, scope: str = "") -> str:
"""
Request the Casdoor server to get access_token.
:param refresh_token: refresh_token for send to Casdoor
:param scope: OAuth scope
:return: Response from Casdoor
"""
r = self.refresh_token_request(refresh_token, scope)
access_token = r.json().get("access_token")

return access_token
Expand Down
20 changes: 20 additions & 0 deletions src/tests/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from requests import Response
from src.casdoor.main import CasdoorSDK, User
from unittest import TestCase

Expand Down Expand Up @@ -45,6 +46,25 @@ def test_get_oauth_token(self):
access_token = sdk.get_oauth_token(self.code)
self.assertIsInstance(access_token, str)

def test_oauth_token_request(self):
sdk = self.get_sdk()
response = sdk.oauth_token_request(self.code)
self.assertIsInstance(response, Response)

def test_refresh_token_request(self):
sdk = self.get_sdk()
response = sdk.oauth_token_request(self.code)
refresh_token = response.json().get("refresh_token")
response = sdk.refresh_token_request(refresh_token)
self.assertIsInstance(response, Response)

def test_get_oauth_refreshed_token(self):
sdk = self.get_sdk()
response = sdk.oauth_token_request(self.code)
refresh_token = response.json().get("refresh_token")
response = sdk.refresh_oauth_token(refresh_token)
self.assertIsInstance(response, str)

def test_parse_jwt_token(self):
sdk = self.get_sdk()
access_token = sdk.get_oauth_token(self.code)
Expand Down

0 comments on commit 352d47e

Please sign in to comment.