Skip to content

Commit f7120e1

Browse files
authored
Merge pull request #82 from getAlby/task-token
feat: add access token expiry handler
2 parents 7b068ec + e646664 commit f7120e1

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ to access the Alby Wallet API in their name. Possible use-cases include:
1515
All examples are using [httpie](https://httpie.io)
1616
- Make a POST request to the oauth server in order to get an access code. This should be made from the browser, as the responds redirects the client back to the client application.
1717
```
18-
http -f POST https://api.regtest.getalby.com/oauth/authorize\?client_id=test_client\&response_type=code\&redirect_uri=localhost:8080/client_app\&scope\=balance:read login=$login password=$password
18+
http -f POST https://api.regtest.getalby.com/oauth/authorize\?client_id=test_client\&response_type=code\&redirect_uri=localhost:8080/client_app\&scope\=balance:read login=$login password=$password expires_in=<optional, token expiry in seconds>
1919
```
2020
- `redirect_uri` should be a web or native uri where the client should be redirected once the authorization is complete.
2121
- You will need a `client_id` and a `client_secret`. For regtest, you can use `test_client` and `test_secret`.
@@ -25,6 +25,7 @@ All examples are using [httpie](https://httpie.io)
2525
- `$login` and `$password` should be your LNDHub login and password.
2626
The response should be a `302 Found` with the `Location` header equal to the redirect URL with the code in it:
2727
`Location: localhost:8080/client_app?code=YOUR_CODE`
28+
- The `expires_in` parameter (optional) allows you to specify the expiry duration of the token in seconds.
2829
- Fetch an access token and a refresh token using the authorization code obtained in the previous step `oauth/token` by doing a HTTP POST request with form parameters:
2930
```
3031
http -a test_client:test_secret

integration_tests/create_token_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func fetchCode(id, redirect, scope string, controller *controllers.OAuthControll
8282
values.Add("scope", scope)
8383
values.Add("login", testAccountLogin)
8484
values.Add("password", testAccountPassword)
85+
values.Add("expires_in", "3600")
8586
req, err := http.NewRequest("POST", "/oauth/authorize", strings.NewReader(values.Encode()))
8687
if err != nil {
8788
return nil, err

service/service.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ func CombinedClientInfoHandler(r *http.Request) (clientID, clientSecret string,
4040
return
4141
}
4242

43+
func (svc *Service) AccessTokenExpHandler(w http.ResponseWriter, r *http.Request) (exp time.Duration, err error) {
44+
expiry := r.FormValue("expires_in")
45+
if expiry != "" {
46+
expiresIn, err := strconv.Atoi(expiry)
47+
if err != nil {
48+
return time.Duration(0), err
49+
}
50+
return time.Duration(expiresIn) * time.Second, nil
51+
}
52+
return time.Duration(svc.Config.AccessTokenExpSeconds) * time.Second, nil
53+
}
54+
4355
func InitService(conf *Config) (svc *Service, err error) {
4456
manager := manage.NewDefaultManager()
4557
manager.SetAuthorizeCodeTokenCfg(manage.DefaultAuthorizeCodeTokenCfg)
@@ -77,6 +89,7 @@ func InitService(conf *Config) (svc *Service, err error) {
7789
Config: conf,
7890
ClientStore: clientStore,
7991
}
92+
srv.AccessTokenExpHandler = svc.AccessTokenExpHandler
8093
return svc, nil
8194
}
8295

0 commit comments

Comments
 (0)