2023-08-10 09:06:00 +00:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-01-28 13:13:04 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2023-08-07 18:47:30 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-01-28 13:13:04 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
)
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// AuthInterceptor is a client interceptor for authentication.
|
2023-01-28 13:13:04 +00:00
|
|
|
type AuthInterceptor struct {
|
|
|
|
authClient *AuthClient
|
|
|
|
accessToken string
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// NewAuthInterceptor returns a new auth interceptor.
|
2023-01-28 13:13:04 +00:00
|
|
|
func NewAuthInterceptor(
|
|
|
|
authClient *AuthClient,
|
|
|
|
refreshDuration time.Duration,
|
|
|
|
) (*AuthInterceptor, error) {
|
|
|
|
interceptor := &AuthInterceptor{
|
|
|
|
authClient: authClient,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := interceptor.scheduleRefreshToken(refreshDuration)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return interceptor, nil
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// Unary returns a client interceptor to authenticate unary RPC.
|
2023-01-28 13:13:04 +00:00
|
|
|
func (interceptor *AuthInterceptor) Unary() grpc.UnaryClientInterceptor {
|
|
|
|
return func(
|
|
|
|
ctx context.Context,
|
|
|
|
method string,
|
2023-11-12 17:23:48 +00:00
|
|
|
req, reply any,
|
2023-01-28 13:13:04 +00:00
|
|
|
cc *grpc.ClientConn,
|
|
|
|
invoker grpc.UnaryInvoker,
|
|
|
|
opts ...grpc.CallOption,
|
|
|
|
) error {
|
|
|
|
return invoker(interceptor.attachToken(ctx), method, req, reply, cc, opts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-13 20:58:21 +00:00
|
|
|
// Stream returns a client interceptor to authenticate stream RPC.
|
2023-01-28 13:13:04 +00:00
|
|
|
func (interceptor *AuthInterceptor) Stream() grpc.StreamClientInterceptor {
|
|
|
|
return func(
|
|
|
|
ctx context.Context,
|
|
|
|
desc *grpc.StreamDesc,
|
|
|
|
cc *grpc.ClientConn,
|
|
|
|
method string,
|
|
|
|
streamer grpc.Streamer,
|
|
|
|
opts ...grpc.CallOption,
|
|
|
|
) (grpc.ClientStream, error) {
|
|
|
|
return streamer(interceptor.attachToken(ctx), desc, cc, method, opts...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (interceptor *AuthInterceptor) attachToken(ctx context.Context) context.Context {
|
|
|
|
return metadata.AppendToOutgoingContext(ctx, "token", interceptor.accessToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (interceptor *AuthInterceptor) scheduleRefreshToken(refreshDuration time.Duration) error {
|
|
|
|
err := interceptor.refreshToken()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
wait := refreshDuration
|
|
|
|
for {
|
|
|
|
time.Sleep(wait)
|
|
|
|
err := interceptor.refreshToken()
|
|
|
|
if err != nil {
|
|
|
|
wait = time.Second
|
|
|
|
} else {
|
|
|
|
wait = refreshDuration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (interceptor *AuthInterceptor) refreshToken() error {
|
|
|
|
accessToken, _, err := interceptor.authClient.Auth()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
interceptor.accessToken = accessToken
|
2023-09-16 08:53:37 +00:00
|
|
|
log.Trace().Msg("token refreshed")
|
2023-01-28 13:13:04 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|