Skip to content

Commit 01d6b5d

Browse files
committed
use oauth-manager from cloud
1 parent 52a188e commit 01d6b5d

12 files changed

Lines changed: 174 additions & 10 deletions

File tree

cloud2cloud-connector/service/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"fmt"
66
"time"
77

8+
"github.com/plgd-dev/cloud/pkg/security/oauth/manager"
89
"github.com/plgd-dev/kit/net/grpc"
9-
"github.com/plgd-dev/kit/security/oauth/manager"
1010
)
1111

1212
type TaskProcessorConfig struct {

cloud2cloud-connector/service/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313

1414
connectorStore "github.com/plgd-dev/cloud/cloud2cloud-connector/store"
1515
"github.com/plgd-dev/cloud/cloud2cloud-connector/uri"
16+
"github.com/plgd-dev/cloud/pkg/security/oauth/manager"
1617
"github.com/plgd-dev/kit/log"
1718
kitNetHttp "github.com/plgd-dev/kit/net/http"
18-
"github.com/plgd-dev/kit/security/oauth/manager"
1919
"google.golang.org/grpc/credentials"
2020

2121
pbAS "github.com/plgd-dev/cloud/authorization/pb"

cloud2cloud-gateway/service/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"fmt"
66
"time"
77

8+
"github.com/plgd-dev/cloud/pkg/security/oauth/manager"
89
"github.com/plgd-dev/kit/net/grpc"
9-
"github.com/plgd-dev/kit/security/oauth/manager"
1010
)
1111

1212
//Config represent application configuration

cloud2cloud-gateway/service/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"net/http"
88
"sync"
99

10+
"github.com/plgd-dev/cloud/pkg/security/oauth/manager"
1011
"github.com/plgd-dev/kit/log"
11-
"github.com/plgd-dev/kit/security/oauth/manager"
1212

1313
"github.com/plgd-dev/cloud/cloud2cloud-gateway/store"
1414
"google.golang.org/grpc"

coap-gateway/service/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package service
33
import (
44
"time"
55

6-
"github.com/plgd-dev/kit/security/oauth/manager"
6+
"github.com/plgd-dev/cloud/pkg/security/oauth/manager"
77
)
88

99
// Config for application.

coap-gateway/service/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"syscall"
1313
"time"
1414

15-
"github.com/plgd-dev/kit/security/oauth/manager"
15+
"github.com/plgd-dev/cloud/pkg/security/oauth/manager"
1616

1717
"google.golang.org/grpc"
1818
"google.golang.org/grpc/credentials"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package manager
2+
3+
import (
4+
"net/url"
5+
"time"
6+
7+
"golang.org/x/oauth2/clientcredentials"
8+
)
9+
10+
type Endpoint struct {
11+
TokenURL string `envconfig:"TOKEN_URL" env:"TOKEN_URL"`
12+
}
13+
14+
type Config struct {
15+
ClientID string `envconfig:"CLIENT_ID" env:"CLIENT_ID"`
16+
ClientSecret string `envconfig:"CLIENT_SECRET" env:"CLIENT_SECRET"`
17+
Scopes []string `envconfig:"SCOPES" env:"SCOPES"`
18+
Endpoint Endpoint `envconfig:"ENDPOINT" env:"ENDPOINT"`
19+
Audience string `envconfig:"AUDIENCE" env:"AUDIENCE"`
20+
RequestTimeout time.Duration `envconfig:"REQUEST_TIMEOUT" env:"REQUEST_TIMEOUT" default:"10s"`
21+
TickFrequency time.Duration `envconfig:"TICK_FREQUENCY" env:"TICK_FREQUENCY" long:"tick-frequency" description:"how frequently we should check whether our token needs renewal" default:"15s"`
22+
}
23+
24+
// ToClientCrendtials converts to clientcredentials.Config
25+
func (c Config) ToClientCrendtials() clientcredentials.Config {
26+
v := make(url.Values)
27+
if c.Audience != "" {
28+
v.Set("audience", c.Audience)
29+
}
30+
31+
return clientcredentials.Config{
32+
ClientID: c.ClientID,
33+
ClientSecret: c.ClientSecret,
34+
Scopes: c.Scopes,
35+
TokenURL: c.Endpoint.TokenURL,
36+
EndpointParams: v,
37+
}
38+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package manager
2+
3+
import (
4+
"context"
5+
"crypto/tls"
6+
"net/http"
7+
"sync"
8+
"time"
9+
10+
"golang.org/x/oauth2/clientcredentials"
11+
12+
"github.com/plgd-dev/kit/log"
13+
"golang.org/x/oauth2"
14+
)
15+
16+
// Manager holds certificates from filesystem watched for changes
17+
type Manager struct {
18+
mutex sync.Mutex
19+
config clientcredentials.Config
20+
requestTimeout time.Duration
21+
tickFrequency time.Duration
22+
startRefreshToken time.Time
23+
token *oauth2.Token
24+
httpClient *http.Client
25+
tokenErr error
26+
doneWg sync.WaitGroup
27+
done chan struct{}
28+
}
29+
30+
// NewManagerFromConfiguration creates a new oauth manager which refreshing token.
31+
func NewManagerFromConfiguration(config Config, tlsCfg *tls.Config) (*Manager, error) {
32+
cfg := config.ToClientCrendtials()
33+
t := http.DefaultTransport.(*http.Transport).Clone()
34+
t.MaxIdleConns = 1
35+
t.MaxConnsPerHost = 1
36+
t.MaxIdleConnsPerHost = 1
37+
t.IdleConnTimeout = time.Second * 30
38+
t.TLSClientConfig = tlsCfg
39+
httpClient := &http.Client{
40+
Transport: t,
41+
Timeout: config.RequestTimeout,
42+
}
43+
token, startRefreshToken, err := getToken(cfg, httpClient, config.RequestTimeout)
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
mgr := &Manager{
49+
config: cfg,
50+
token: token,
51+
startRefreshToken: startRefreshToken,
52+
requestTimeout: config.RequestTimeout,
53+
httpClient: httpClient,
54+
tickFrequency: config.TickFrequency,
55+
56+
done: make(chan struct{}),
57+
}
58+
mgr.doneWg.Add(1)
59+
60+
go mgr.watchToken()
61+
62+
return mgr, nil
63+
}
64+
65+
// GetToken returns token for clients
66+
func (a *Manager) GetToken(ctx context.Context) (*oauth2.Token, error) {
67+
a.mutex.Lock()
68+
defer a.mutex.Unlock()
69+
return a.token, a.tokenErr
70+
}
71+
72+
// Close ends watching token
73+
func (a *Manager) Close() {
74+
if a.done != nil {
75+
close(a.done)
76+
a.doneWg.Wait()
77+
}
78+
}
79+
80+
func (a *Manager) shouldRefresh() bool {
81+
return time.Now().After(a.startRefreshToken)
82+
}
83+
84+
func getToken(cfg clientcredentials.Config, httpClient *http.Client, requestTimeout time.Duration) (*oauth2.Token, time.Time, error) {
85+
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
86+
defer cancel()
87+
88+
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
89+
90+
token, err := cfg.Token(ctx)
91+
var startRefreshToken time.Time
92+
if err == nil {
93+
now := time.Now()
94+
startRefreshToken = now.Add(token.Expiry.Sub(now) * 2 / 3)
95+
}
96+
return token, startRefreshToken, err
97+
}
98+
99+
func (a *Manager) refreshToken() {
100+
token, startRefreshToken, err := getToken(a.config, a.httpClient, a.requestTimeout)
101+
if err != nil {
102+
log.Errorf("cannot refresh token: %v", err)
103+
}
104+
a.mutex.Lock()
105+
defer a.mutex.Unlock()
106+
a.token = token
107+
a.tokenErr = err
108+
a.startRefreshToken = startRefreshToken
109+
}
110+
111+
func (a *Manager) watchToken() {
112+
defer a.doneWg.Done()
113+
t := time.NewTicker(a.tickFrequency)
114+
defer t.Stop()
115+
116+
for {
117+
select {
118+
case <-a.done:
119+
return
120+
case <-t.C:
121+
if a.shouldRefresh() {
122+
a.refreshToken()
123+
}
124+
}
125+
}
126+
}

resource-aggregate/service/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"fmt"
66
"time"
77

8+
"github.com/plgd-dev/cloud/pkg/security/oauth/manager"
89
"github.com/plgd-dev/kit/net/grpc"
9-
"github.com/plgd-dev/kit/security/oauth/manager"
1010
)
1111

1212
//Config represent application configuration

resource-aggregate/service/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import (
1616

1717
clientAS "github.com/plgd-dev/cloud/authorization/client"
1818
pbAS "github.com/plgd-dev/cloud/authorization/pb"
19+
"github.com/plgd-dev/cloud/pkg/security/oauth/manager"
1920
cqrsEventBus "github.com/plgd-dev/cloud/resource-aggregate/cqrs/eventbus"
2021
cqrsEventStore "github.com/plgd-dev/cloud/resource-aggregate/cqrs/eventstore"
2122
cqrsMaintenance "github.com/plgd-dev/cloud/resource-aggregate/cqrs/eventstore/maintenance"
2223
"github.com/plgd-dev/cloud/resource-aggregate/pb"
2324
"github.com/plgd-dev/kit/log"
2425
kitNetGrpc "github.com/plgd-dev/kit/net/grpc"
2526
"github.com/plgd-dev/kit/security/jwt"
26-
"github.com/plgd-dev/kit/security/oauth/manager"
2727
"golang.org/x/sync/semaphore"
2828
"google.golang.org/grpc"
2929
"google.golang.org/grpc/credentials"

0 commit comments

Comments
 (0)