Skip to content

Commit

Permalink
✨ Feature: support oauth token auth (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Jul 14, 2024
1 parent 8f41ba1 commit 320613a
Show file tree
Hide file tree
Showing 7 changed files with 1,127 additions and 604 deletions.
93 changes: 91 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ from githubkit import GitHub, OAuthAppAuthStrategy
github = GitHub(OAuthAppAuthStrategy("<client_id_here>", "<client_secret_here>"))
```

or using GitHub APP / OAuth APP token authentication (This is usefull when you stored the user token in a database):

```python
from githubkit import GitHub, OAuthTokenAuthStrategy

github = GitHub(
OAuthTokenAuthStrategy(
"<client_id_here>",
"<client_secret_here>",
"<access_token_here>",
"<access_token_expire_time_here>",
"<refresh_token_here>",
"<refresh_token_expire_time_here>",
)
)
```

or using GitHub APP / OAuth APP web flow authentication:

```python
Expand All @@ -147,6 +164,22 @@ github = GitHub(
)
```

or using GitHub APP / OAuth APP device flow authentication:

```python
from githubkit import GitHub, OAuthDeviceAuthStrategy

# sync/async func for displaying user code to user
def callback(data: dict):
print(data["user_code"])

github = GitHub(
OAuthDeviceAuthStrategy(
"<client_id_here>", callback
)
)
```

or using GitHub Action authentication:

```python
Expand Down Expand Up @@ -503,7 +536,7 @@ from githubkit import GitHub
event = GitHub.webhooks("2022-11-28").parse(request.headers["X-GitHub-Event"], request.body)
```

### Switch between AuthStrategy
### Switch between AuthStrategy (Installation, OAuth Web/Device Flow)

You can change the auth strategy and get a new client simplely using `with_auth`.

Expand All @@ -518,13 +551,69 @@ installation_github = github.with_auth(
)
```

Change from `OAuthAppAuthStrategy` to `OAuthWebAuthStrategy`:
Change from `OAuthAppAuthStrategy` to `OAuthWebAuthStrategy` (OAuth Web Flow):

```python
from githubkit import GitHub, OAuthAppAuthStrategy
github = GitHub(OAuthAppAuthStrategy("<client_id>", "<client_secret>"))
user_github = github.with_auth(github.auth.as_web_user("<code>"))
# now you can act as the user
resp = user_github.rest.users.get_authenticated()
user = resp.parsed_data
# you can get the user token after you maked a request as user
user_token = user_github.auth.token
user_token_expire_time = user_github.auth.expire_time
refresh_token = user_github.auth.refresh_token
refresh_token_expire_time = user_github.auth.refresh_token_expire_time
```

you can also get the user token directly without making a request (Change from `OAuthWebAuthStrategy` to `OAuthTokenAuthStrategy`):

```python
auth: OAuthTokenAuthStrategy = github.auth.as_web_user("<code>").exchange_token(github)
# or asynchronously
auth: OAuthTokenAuthStrategy = await github.auth.as_web_user("<code>").async_exchange_token(github)
user_token = auth.token
user_token_expire_time = auth.expire_time
refresh_token = auth.refresh_token
refresh_token_expire_time = auth.refresh_token_expire_time
user_github = github.with_auth(auth)
```

Change from `OAuthDeviceAuthStrategy` to `OAuthTokenAuthStrategy`:

```python
from githubkit import GitHub, OAuthDeviceAuthStrategy
def callback(data: dict):
print(data["user_code"])
user_github = GitHub(OAuthDeviceAuthStrategy("<client_id>", callback))
# now you can act as the user
resp = user_github.rest.users.get_authenticated()
user = resp.parsed_data
# you can get the user token after you maked a request as user
user_token = user_github.auth.token
user_token_expire_time = user_github.auth.expire_time
refresh_token = user_github.auth.refresh_token
refresh_token_expire_time = user_github.auth.refresh_token_expire_time
# you can also exchange the token directly without making a request
auth: OAuthTokenAuthStrategy = github.auth.exchange_token(github)
# or asynchronously
auth: OAuthTokenAuthStrategy = await github.auth.async_exchange_token(github)
user_token = auth.token
user_token_expire_time = auth.expire_time
refresh_token = auth.refresh_token
refresh_token_expire_time = auth.refresh_token_expire_time
user_github = github.with_auth(auth)
```

## Development
Expand Down
Loading

0 comments on commit 320613a

Please sign in to comment.