mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-24 01:10:31 +00:00
d96032349a
Co-authored-by: 6543 <6543@obermui.de>
45 lines
918 B
Go
45 lines
918 B
Go
package rpc
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/rpc/proto"
|
|
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
type AuthClient struct {
|
|
client proto.WoodpeckerAuthClient
|
|
conn *grpc.ClientConn
|
|
agentToken string
|
|
agentID int64
|
|
}
|
|
|
|
func NewAuthGrpcClient(conn *grpc.ClientConn, agentToken string, agentID int64) *AuthClient {
|
|
client := new(AuthClient)
|
|
client.client = proto.NewWoodpeckerAuthClient(conn)
|
|
client.conn = conn
|
|
client.agentToken = agentToken
|
|
client.agentID = agentID
|
|
return client
|
|
}
|
|
|
|
func (c *AuthClient) Auth() (string, int64, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
req := &proto.AuthRequest{
|
|
AgentToken: c.agentToken,
|
|
AgentId: c.agentID,
|
|
}
|
|
|
|
res, err := c.client.Auth(ctx, req)
|
|
if err != nil {
|
|
return "", -1, err
|
|
}
|
|
|
|
c.agentID = res.GetAgentId()
|
|
|
|
return res.GetAccessToken(), c.agentID, nil
|
|
}
|