oauth2: correctly populate token expiry data

* Populate the field when needed
* Convert it to a time instead of a duration
This commit is contained in:
Simone Gotti 2019-04-29 14:57:07 +02:00
parent 52bcf096c7
commit 95e73e66a0
4 changed files with 169 additions and 145 deletions

View File

@ -18,6 +18,7 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"strconv" "strconv"
"time"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sorintlab/agola/internal/db" "github.com/sorintlab/agola/internal/db"
@ -130,12 +131,13 @@ func (h *CreateUserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
if req.CreateUserLARequest != nil { if req.CreateUserLARequest != nil {
creq.CreateUserLARequest = &command.CreateUserLARequest{ creq.CreateUserLARequest = &command.CreateUserLARequest{
RemoteSourceName: req.CreateUserLARequest.RemoteSourceName, RemoteSourceName: req.CreateUserLARequest.RemoteSourceName,
RemoteUserID: req.CreateUserLARequest.RemoteUserID, RemoteUserID: req.CreateUserLARequest.RemoteUserID,
RemoteUserName: req.CreateUserLARequest.RemoteUserName, RemoteUserName: req.CreateUserLARequest.RemoteUserName,
Oauth2AccessToken: req.CreateUserLARequest.Oauth2AccessToken, UserAccessToken: req.CreateUserLARequest.UserAccessToken,
Oauth2RefreshToken: req.CreateUserLARequest.Oauth2RefreshToken, Oauth2AccessToken: req.CreateUserLARequest.Oauth2AccessToken,
UserAccessToken: req.CreateUserLARequest.UserAccessToken, Oauth2RefreshToken: req.CreateUserLARequest.Oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: req.CreateUserLARequest.Oauth2AccessTokenExpiresAt,
} }
} }
@ -299,12 +301,13 @@ func (h *UsersHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
type CreateUserLARequest struct { type CreateUserLARequest struct {
RemoteSourceName string `json:"remote_source_name"` RemoteSourceName string `json:"remote_source_name"`
RemoteUserID string `json:"remote_user_id"` RemoteUserID string `json:"remote_user_id"`
RemoteUserName string `json:"remote_user_name"` RemoteUserName string `json:"remote_user_name"`
UserAccessToken string `json:"user_access_token"` UserAccessToken string `json:"user_access_token"`
Oauth2AccessToken string `json:"oauth2_access_token"` Oauth2AccessToken string `json:"oauth2_access_token"`
Oauth2RefreshToken string `json:"oauth2_refresh_token"` Oauth2RefreshToken string `json:"oauth2_refresh_token"`
Oauth2AccessTokenExpiresAt time.Time `json:"oauth_2_access_token_expires_at"`
} }
type CreateUserLAHandler struct { type CreateUserLAHandler struct {
@ -329,13 +332,14 @@ func (h *CreateUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
} }
creq := &command.CreateUserLARequest{ creq := &command.CreateUserLARequest{
UserName: userName, UserName: userName,
RemoteSourceName: req.RemoteSourceName, RemoteSourceName: req.RemoteSourceName,
RemoteUserID: req.RemoteUserID, RemoteUserID: req.RemoteUserID,
RemoteUserName: req.RemoteUserName, RemoteUserName: req.RemoteUserName,
Oauth2AccessToken: req.Oauth2AccessToken, UserAccessToken: req.UserAccessToken,
Oauth2RefreshToken: req.Oauth2RefreshToken, Oauth2AccessToken: req.Oauth2AccessToken,
UserAccessToken: req.UserAccessToken, Oauth2RefreshToken: req.Oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: req.Oauth2AccessTokenExpiresAt,
} }
user, err := h.ch.CreateUserLA(ctx, creq) user, err := h.ch.CreateUserLA(ctx, creq)
if httpError(w, err) { if httpError(w, err) {
@ -373,11 +377,12 @@ func (h *DeleteUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
} }
type UpdateUserLARequest struct { type UpdateUserLARequest struct {
RemoteUserID string `json:"remote_user_id"` RemoteUserID string `json:"remote_user_id"`
RemoteUserName string `json:"remote_user_name"` RemoteUserName string `json:"remote_user_name"`
UserAccessToken string `json:"user_access_token"` UserAccessToken string `json:"user_access_token"`
Oauth2AccessToken string `json:"oauth2_access_token"` Oauth2AccessToken string `json:"oauth2_access_token"`
Oauth2RefreshToken string `json:"oauth2_refresh_token"` Oauth2RefreshToken string `json:"oauth2_refresh_token"`
Oauth2AccessTokenExpiresAt time.Time `json:"oauth_2_access_token_expires_at"`
} }
type UpdateUserLAHandler struct { type UpdateUserLAHandler struct {
@ -403,13 +408,14 @@ func (h *UpdateUserLAHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
} }
creq := &command.UpdateUserLARequest{ creq := &command.UpdateUserLARequest{
UserName: userName, UserName: userName,
LinkedAccountID: linkedAccountID, LinkedAccountID: linkedAccountID,
RemoteUserID: req.RemoteUserID, RemoteUserID: req.RemoteUserID,
RemoteUserName: req.RemoteUserName, RemoteUserName: req.RemoteUserName,
Oauth2AccessToken: req.Oauth2AccessToken, UserAccessToken: req.UserAccessToken,
Oauth2RefreshToken: req.Oauth2RefreshToken, Oauth2AccessToken: req.Oauth2AccessToken,
UserAccessToken: req.UserAccessToken, Oauth2RefreshToken: req.Oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: req.Oauth2AccessTokenExpiresAt,
} }
user, err := h.ch.UpdateUserLA(ctx, creq) user, err := h.ch.UpdateUserLA(ctx, creq)
if httpError(w, err) { if httpError(w, err) {

View File

@ -18,6 +18,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"path" "path"
"time"
"github.com/sorintlab/agola/internal/datamanager" "github.com/sorintlab/agola/internal/datamanager"
"github.com/sorintlab/agola/internal/db" "github.com/sorintlab/agola/internal/db"
@ -309,13 +310,14 @@ func (s *CommandHandler) CreateUser(ctx context.Context, req *CreateUserRequest)
} }
la := &types.LinkedAccount{ la := &types.LinkedAccount{
ID: uuid.NewV4().String(), ID: uuid.NewV4().String(),
RemoteSourceID: rs.ID, RemoteSourceID: rs.ID,
RemoteUserID: req.CreateUserLARequest.RemoteUserID, RemoteUserID: req.CreateUserLARequest.RemoteUserID,
RemoteUserName: req.CreateUserLARequest.RemoteUserName, RemoteUserName: req.CreateUserLARequest.RemoteUserName,
UserAccessToken: req.CreateUserLARequest.UserAccessToken, UserAccessToken: req.CreateUserLARequest.UserAccessToken,
Oauth2AccessToken: req.CreateUserLARequest.Oauth2AccessToken, Oauth2AccessToken: req.CreateUserLARequest.Oauth2AccessToken,
Oauth2RefreshToken: req.CreateUserLARequest.Oauth2RefreshToken, Oauth2RefreshToken: req.CreateUserLARequest.Oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: req.CreateUserLARequest.Oauth2AccessTokenExpiresAt,
} }
user.LinkedAccounts[la.ID] = la user.LinkedAccounts[la.ID] = la
@ -400,13 +402,14 @@ func (s *CommandHandler) DeleteUser(ctx context.Context, userName string) error
} }
type CreateUserLARequest struct { type CreateUserLARequest struct {
UserName string UserName string
RemoteSourceName string RemoteSourceName string
RemoteUserID string RemoteUserID string
RemoteUserName string RemoteUserName string
UserAccessToken string UserAccessToken string
Oauth2AccessToken string Oauth2AccessToken string
Oauth2RefreshToken string Oauth2RefreshToken string
Oauth2AccessTokenExpiresAt time.Time
} }
func (s *CommandHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequest) (*types.LinkedAccount, error) { func (s *CommandHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequest) (*types.LinkedAccount, error) {
@ -466,13 +469,14 @@ func (s *CommandHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequ
} }
la := &types.LinkedAccount{ la := &types.LinkedAccount{
ID: uuid.NewV4().String(), ID: uuid.NewV4().String(),
RemoteSourceID: rs.ID, RemoteSourceID: rs.ID,
RemoteUserID: req.RemoteUserID, RemoteUserID: req.RemoteUserID,
RemoteUserName: req.RemoteUserName, RemoteUserName: req.RemoteUserName,
UserAccessToken: req.UserAccessToken, UserAccessToken: req.UserAccessToken,
Oauth2AccessToken: req.Oauth2AccessToken, Oauth2AccessToken: req.Oauth2AccessToken,
Oauth2RefreshToken: req.Oauth2RefreshToken, Oauth2RefreshToken: req.Oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: req.Oauth2AccessTokenExpiresAt,
} }
user.LinkedAccounts[la.ID] = la user.LinkedAccounts[la.ID] = la
@ -555,13 +559,14 @@ func (s *CommandHandler) DeleteUserLA(ctx context.Context, userName, laID string
} }
type UpdateUserLARequest struct { type UpdateUserLARequest struct {
UserName string UserName string
LinkedAccountID string LinkedAccountID string
RemoteUserID string RemoteUserID string
RemoteUserName string RemoteUserName string
UserAccessToken string UserAccessToken string
Oauth2AccessToken string Oauth2AccessToken string
Oauth2RefreshToken string Oauth2RefreshToken string
Oauth2AccessTokenExpiresAt time.Time
} }
func (s *CommandHandler) UpdateUserLA(ctx context.Context, req *UpdateUserLARequest) (*types.LinkedAccount, error) { func (s *CommandHandler) UpdateUserLA(ctx context.Context, req *UpdateUserLARequest) (*types.LinkedAccount, error) {
@ -617,6 +622,7 @@ func (s *CommandHandler) UpdateUserLA(ctx context.Context, req *UpdateUserLARequ
la.UserAccessToken = req.UserAccessToken la.UserAccessToken = req.UserAccessToken
la.Oauth2AccessToken = req.Oauth2AccessToken la.Oauth2AccessToken = req.Oauth2AccessToken
la.Oauth2RefreshToken = req.Oauth2RefreshToken la.Oauth2RefreshToken = req.Oauth2RefreshToken
la.Oauth2AccessTokenExpiresAt = req.Oauth2AccessTokenExpiresAt
userj, err := json.Marshal(user) userj, err := json.Marshal(user)
if err != nil { if err != nil {

View File

@ -17,6 +17,7 @@ package command
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"time"
gitsource "github.com/sorintlab/agola/internal/gitsources" gitsource "github.com/sorintlab/agola/internal/gitsources"
csapi "github.com/sorintlab/agola/internal/services/configstore/api" csapi "github.com/sorintlab/agola/internal/services/configstore/api"
@ -100,11 +101,12 @@ func (c *CommandHandler) CreateUserToken(ctx context.Context, req *CreateUserTok
} }
type CreateUserLARequest struct { type CreateUserLARequest struct {
UserName string UserName string
RemoteSourceName string RemoteSourceName string
RemoteSourceUserAccessToken string UserAccessToken string
RemoteSourceOauth2AccessToken string Oauth2AccessToken string
RemoteSourceOauth2RefreshToken string Oauth2RefreshToken string
Oauth2AccessTokenExpiresAt time.Time
} }
func (c *CommandHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequest) (*types.LinkedAccount, error) { func (c *CommandHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequest) (*types.LinkedAccount, error) {
@ -130,7 +132,7 @@ func (c *CommandHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequ
return nil, util.NewErrBadRequest(errors.Errorf("user %q already have a linked account for remote source %q", userName, rs.Name)) return nil, util.NewErrBadRequest(errors.Errorf("user %q already have a linked account for remote source %q", userName, rs.Name))
} }
accessToken, err := common.GetAccessToken(rs.AuthType, req.RemoteSourceUserAccessToken, req.RemoteSourceOauth2AccessToken) accessToken, err := common.GetAccessToken(rs.AuthType, req.UserAccessToken, req.Oauth2AccessToken)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -148,12 +150,13 @@ func (c *CommandHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequ
} }
creq := &csapi.CreateUserLARequest{ creq := &csapi.CreateUserLARequest{
RemoteSourceName: req.RemoteSourceName, RemoteSourceName: req.RemoteSourceName,
RemoteUserID: remoteUserInfo.ID, RemoteUserID: remoteUserInfo.ID,
RemoteUserName: remoteUserInfo.LoginName, RemoteUserName: remoteUserInfo.LoginName,
Oauth2AccessToken: req.RemoteSourceOauth2AccessToken, UserAccessToken: req.UserAccessToken,
Oauth2RefreshToken: req.RemoteSourceOauth2RefreshToken, Oauth2AccessToken: req.Oauth2AccessToken,
UserAccessToken: req.RemoteSourceUserAccessToken, Oauth2RefreshToken: req.Oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: req.Oauth2AccessTokenExpiresAt,
} }
c.log.Infof("creating linked account") c.log.Infof("creating linked account")
@ -167,11 +170,12 @@ func (c *CommandHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequ
} }
type RegisterUserRequest struct { type RegisterUserRequest struct {
UserName string UserName string
RemoteSourceName string RemoteSourceName string
RemoteSourceUserAccessToken string UserAccessToken string
RemoteSourceOauth2AccessToken string Oauth2AccessToken string
RemoteSourceOauth2RefreshToken string Oauth2RefreshToken string
Oauth2AccessTokenExpiresAt time.Time
} }
func (c *CommandHandler) RegisterUser(ctx context.Context, req *RegisterUserRequest) (*types.User, error) { func (c *CommandHandler) RegisterUser(ctx context.Context, req *RegisterUserRequest) (*types.User, error) {
@ -188,7 +192,7 @@ func (c *CommandHandler) RegisterUser(ctx context.Context, req *RegisterUserRequ
} }
c.log.Infof("rs: %s", util.Dump(rs)) c.log.Infof("rs: %s", util.Dump(rs))
accessToken, err := common.GetAccessToken(rs.AuthType, req.RemoteSourceUserAccessToken, req.RemoteSourceOauth2AccessToken) accessToken, err := common.GetAccessToken(rs.AuthType, req.UserAccessToken, req.Oauth2AccessToken)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -208,12 +212,13 @@ func (c *CommandHandler) RegisterUser(ctx context.Context, req *RegisterUserRequ
creq := &csapi.CreateUserRequest{ creq := &csapi.CreateUserRequest{
UserName: req.UserName, UserName: req.UserName,
CreateUserLARequest: &csapi.CreateUserLARequest{ CreateUserLARequest: &csapi.CreateUserLARequest{
RemoteSourceName: req.RemoteSourceName, RemoteSourceName: req.RemoteSourceName,
RemoteUserID: remoteUserInfo.ID, RemoteUserID: remoteUserInfo.ID,
RemoteUserName: remoteUserInfo.LoginName, RemoteUserName: remoteUserInfo.LoginName,
Oauth2AccessToken: req.RemoteSourceOauth2AccessToken, UserAccessToken: req.UserAccessToken,
Oauth2RefreshToken: req.RemoteSourceOauth2RefreshToken, Oauth2AccessToken: req.Oauth2AccessToken,
UserAccessToken: req.RemoteSourceUserAccessToken, Oauth2RefreshToken: req.Oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: req.Oauth2AccessTokenExpiresAt,
}, },
} }
@ -228,10 +233,11 @@ func (c *CommandHandler) RegisterUser(ctx context.Context, req *RegisterUserRequ
} }
type LoginUserRequest struct { type LoginUserRequest struct {
RemoteSourceName string RemoteSourceName string
RemoteSourceUserAccessToken string UserAccessToken string
RemoteSourceOauth2AccessToken string Oauth2AccessToken string
RemoteSourceOauth2RefreshToken string Oauth2RefreshToken string
Oauth2AccessTokenExpiresAt time.Time
} }
type LoginUserResponse struct { type LoginUserResponse struct {
@ -246,7 +252,7 @@ func (c *CommandHandler) LoginUser(ctx context.Context, req *LoginUserRequest) (
} }
c.log.Infof("rs: %s", util.Dump(rs)) c.log.Infof("rs: %s", util.Dump(rs))
accessToken, err := common.GetAccessToken(rs.AuthType, req.RemoteSourceUserAccessToken, req.RemoteSourceOauth2AccessToken) accessToken, err := common.GetAccessToken(rs.AuthType, req.UserAccessToken, req.Oauth2AccessToken)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -281,20 +287,21 @@ func (c *CommandHandler) LoginUser(ctx context.Context, req *LoginUserRequest) (
} }
// Update oauth tokens if they have changed since the getuserinfo request may have updated them // Update oauth tokens if they have changed since the getuserinfo request may have updated them
if la.Oauth2AccessToken != req.RemoteSourceOauth2AccessToken || if la.Oauth2AccessToken != req.Oauth2AccessToken ||
la.Oauth2RefreshToken != req.RemoteSourceOauth2RefreshToken || la.Oauth2RefreshToken != req.Oauth2RefreshToken ||
la.UserAccessToken != req.RemoteSourceUserAccessToken { la.UserAccessToken != req.UserAccessToken {
la.Oauth2AccessToken = req.RemoteSourceOauth2AccessToken la.Oauth2AccessToken = req.Oauth2AccessToken
la.Oauth2RefreshToken = req.RemoteSourceOauth2RefreshToken la.Oauth2RefreshToken = req.Oauth2RefreshToken
la.UserAccessToken = req.RemoteSourceUserAccessToken la.UserAccessToken = req.UserAccessToken
creq := &csapi.UpdateUserLARequest{ creq := &csapi.UpdateUserLARequest{
RemoteUserID: la.RemoteUserID, RemoteUserID: la.RemoteUserID,
RemoteUserName: la.RemoteUserName, RemoteUserName: la.RemoteUserName,
Oauth2AccessToken: la.Oauth2AccessToken, UserAccessToken: la.UserAccessToken,
Oauth2RefreshToken: la.Oauth2RefreshToken, Oauth2AccessToken: la.Oauth2AccessToken,
UserAccessToken: la.UserAccessToken, Oauth2RefreshToken: la.Oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: la.Oauth2AccessTokenExpiresAt,
} }
c.log.Infof("updating user %q linked account", user.UserName) c.log.Infof("updating user %q linked account", user.UserName)
@ -317,10 +324,11 @@ func (c *CommandHandler) LoginUser(ctx context.Context, req *LoginUserRequest) (
} }
type AuthorizeRequest struct { type AuthorizeRequest struct {
RemoteSourceName string RemoteSourceName string
RemoteSourceUserAccessToken string UserAccessToken string
RemoteSourceOauth2AccessToken string Oauth2AccessToken string
RemoteSourceOauth2RefreshToken string Oauth2RefreshToken string
Oauth2AccessTokenExpiresAt time.Time
} }
type AuthorizeResponse struct { type AuthorizeResponse struct {
@ -335,7 +343,7 @@ func (c *CommandHandler) Authorize(ctx context.Context, req *AuthorizeRequest) (
} }
c.log.Infof("rs: %s", util.Dump(rs)) c.log.Infof("rs: %s", util.Dump(rs))
accessToken, err := common.GetAccessToken(rs.AuthType, req.RemoteSourceUserAccessToken, req.RemoteSourceOauth2AccessToken) accessToken, err := common.GetAccessToken(rs.AuthType, req.UserAccessToken, req.Oauth2AccessToken)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -433,7 +441,7 @@ func (c *CommandHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourc
if err != nil { if err != nil {
return nil, err return nil, err
} }
cres, err := c.HandleRemoteSourceAuthRequest(ctx, requestType, string(requestj), accessToken, "", "") cres, err := c.HandleRemoteSourceAuthRequest(ctx, requestType, string(requestj), accessToken, "", "", time.Time{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -464,7 +472,7 @@ type CreateUserLAResponse struct {
LinkedAccount *types.LinkedAccount LinkedAccount *types.LinkedAccount
} }
func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requestType RemoteSourceRequestType, requestString string, userAccessToken, Oauth2AccessToken, Oauth2RefreshToken string) (*RemoteSourceAuthResult, error) { func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requestType RemoteSourceRequestType, requestString string, userAccessToken, oauth2AccessToken, oauth2RefreshToken string, oauth2AccessTokenExpiresAt time.Time) (*RemoteSourceAuthResult, error) {
switch requestType { switch requestType {
case RemoteSourceRequestTypeCreateUserLA: case RemoteSourceRequestTypeCreateUserLA:
var req *CreateUserLARequest var req *CreateUserLARequest
@ -473,11 +481,12 @@ func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requ
} }
creq := &CreateUserLARequest{ creq := &CreateUserLARequest{
UserName: req.UserName, UserName: req.UserName,
RemoteSourceName: req.RemoteSourceName, RemoteSourceName: req.RemoteSourceName,
RemoteSourceUserAccessToken: userAccessToken, UserAccessToken: userAccessToken,
RemoteSourceOauth2AccessToken: Oauth2AccessToken, Oauth2AccessToken: oauth2AccessToken,
RemoteSourceOauth2RefreshToken: Oauth2RefreshToken, Oauth2RefreshToken: oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt,
} }
la, err := c.CreateUserLA(ctx, creq) la, err := c.CreateUserLA(ctx, creq)
if err != nil { if err != nil {
@ -490,6 +499,29 @@ func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requ
}, },
}, nil }, nil
case RemoteSourceRequestTypeRegisterUser:
var req *RegisterUserRequest
if err := json.Unmarshal([]byte(requestString), &req); err != nil {
return nil, errors.Errorf("failed to unmarshal request")
}
creq := &RegisterUserRequest{
UserName: req.UserName,
RemoteSourceName: req.RemoteSourceName,
UserAccessToken: userAccessToken,
Oauth2AccessToken: oauth2AccessToken,
Oauth2RefreshToken: oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt,
}
cresp, err := c.RegisterUser(ctx, creq)
if err != nil {
return nil, err
}
return &RemoteSourceAuthResult{
RequestType: requestType,
Response: cresp,
}, nil
case RemoteSourceRequestTypeLoginUser: case RemoteSourceRequestTypeLoginUser:
var req *LoginUserRequest var req *LoginUserRequest
if err := json.Unmarshal([]byte(requestString), &req); err != nil { if err := json.Unmarshal([]byte(requestString), &req); err != nil {
@ -497,10 +529,11 @@ func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requ
} }
creq := &LoginUserRequest{ creq := &LoginUserRequest{
RemoteSourceName: req.RemoteSourceName, RemoteSourceName: req.RemoteSourceName,
RemoteSourceUserAccessToken: userAccessToken, UserAccessToken: userAccessToken,
RemoteSourceOauth2AccessToken: Oauth2AccessToken, Oauth2AccessToken: oauth2AccessToken,
RemoteSourceOauth2RefreshToken: Oauth2RefreshToken, Oauth2RefreshToken: oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt,
} }
cresp, err := c.LoginUser(ctx, creq) cresp, err := c.LoginUser(ctx, creq)
if err != nil { if err != nil {
@ -518,10 +551,11 @@ func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requ
} }
creq := &AuthorizeRequest{ creq := &AuthorizeRequest{
RemoteSourceName: req.RemoteSourceName, RemoteSourceName: req.RemoteSourceName,
RemoteSourceUserAccessToken: userAccessToken, UserAccessToken: userAccessToken,
RemoteSourceOauth2AccessToken: Oauth2AccessToken, Oauth2AccessToken: oauth2AccessToken,
RemoteSourceOauth2RefreshToken: Oauth2RefreshToken, Oauth2RefreshToken: oauth2RefreshToken,
Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt,
} }
cresp, err := c.Authorize(ctx, creq) cresp, err := c.Authorize(ctx, creq)
if err != nil { if err != nil {
@ -532,28 +566,6 @@ func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requ
Response: cresp, Response: cresp,
}, nil }, nil
case RemoteSourceRequestTypeRegisterUser:
var req *RegisterUserRequest
if err := json.Unmarshal([]byte(requestString), &req); err != nil {
return nil, errors.Errorf("failed to unmarshal request")
}
creq := &RegisterUserRequest{
UserName: req.UserName,
RemoteSourceName: req.RemoteSourceName,
RemoteSourceUserAccessToken: userAccessToken,
RemoteSourceOauth2AccessToken: Oauth2AccessToken,
RemoteSourceOauth2RefreshToken: Oauth2RefreshToken,
}
cresp, err := c.RegisterUser(ctx, creq)
if err != nil {
return nil, err
}
return &RemoteSourceAuthResult{
RequestType: requestType,
Response: cresp,
}, nil
default: default:
return nil, errors.Errorf("unknown request") return nil, errors.Errorf("unknown request")
} }
@ -603,5 +615,5 @@ func (c *CommandHandler) HandleOauth2Callback(ctx context.Context, code, state s
return nil, err return nil, err
} }
return c.HandleRemoteSourceAuthRequest(ctx, requestType, requestString, "", oauth2Token.AccessToken, oauth2Token.RefreshToken) return c.HandleRemoteSourceAuthRequest(ctx, requestType, requestString, "", oauth2Token.AccessToken, oauth2Token.RefreshToken, oauth2Token.Expiry)
} }

View File

@ -127,9 +127,9 @@ type LinkedAccount struct {
UserAccessToken string `json:"user_access_token,omitempty"` UserAccessToken string `json:"user_access_token,omitempty"`
Oauth2AccessToken string `json:"oauth2_access_token,omitempty"` Oauth2AccessToken string `json:"oauth2_access_token,omitempty"`
Oauth2RefreshToken string `json:"oauth2_refresh_token,omitempty"` Oauth2RefreshToken string `json:"oauth2_refresh_token,omitempty"`
Oauth2Expire time.Duration `json:"oauth2_expire,omitempty"` Oauth2AccessTokenExpiresAt time.Time `json:"oauth_2_access_token_expires_at,omitempty"`
} }
type Project struct { type Project struct {