2019-02-21 16:58:25 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2019-04-29 12:57:07 +00:00
|
|
|
"time"
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-03-29 16:53:15 +00:00
|
|
|
gitsource "github.com/sorintlab/agola/internal/gitsources"
|
2019-02-21 16:58:25 +00:00
|
|
|
csapi "github.com/sorintlab/agola/internal/services/configstore/api"
|
|
|
|
"github.com/sorintlab/agola/internal/services/gateway/common"
|
|
|
|
"github.com/sorintlab/agola/internal/services/types"
|
|
|
|
"github.com/sorintlab/agola/internal/util"
|
|
|
|
|
|
|
|
jwt "github.com/dgrijalva/jwt-go"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2019-04-29 13:42:10 +00:00
|
|
|
const (
|
|
|
|
expireTimeRange time.Duration = 5 * time.Minute
|
|
|
|
)
|
|
|
|
|
|
|
|
func isAccessTokenExpired(expiresAt time.Time) bool {
|
|
|
|
if expiresAt.IsZero() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return expiresAt.Add(-expireTimeRange).Before(time.Now())
|
|
|
|
}
|
|
|
|
|
2019-03-14 13:36:18 +00:00
|
|
|
type CreateUserRequest struct {
|
|
|
|
UserName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommandHandler) CreateUser(ctx context.Context, req *CreateUserRequest) (*types.User, error) {
|
|
|
|
if req.UserName == "" {
|
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("user name required"))
|
|
|
|
}
|
|
|
|
if !util.ValidateName(req.UserName) {
|
2019-04-08 10:08:31 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("invalid user name %q", req.UserName))
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:50:51 +00:00
|
|
|
creq := &csapi.CreateUserRequest{
|
2019-03-14 13:36:18 +00:00
|
|
|
UserName: req.UserName,
|
|
|
|
}
|
|
|
|
|
|
|
|
c.log.Infof("creating user")
|
2019-04-09 12:53:00 +00:00
|
|
|
u, resp, err := c.configstoreClient.CreateUser(ctx, creq)
|
2019-03-14 13:36:18 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create user"))
|
2019-03-14 13:36:18 +00:00
|
|
|
}
|
2019-04-30 10:56:43 +00:00
|
|
|
c.log.Infof("user %s created, ID: %s", u.Name, u.ID)
|
2019-03-14 13:36:18 +00:00
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2019-04-05 13:56:06 +00:00
|
|
|
type CreateUserTokenRequest struct {
|
2019-05-03 09:07:53 +00:00
|
|
|
UserRef string
|
2019-04-05 13:56:06 +00:00
|
|
|
TokenName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommandHandler) CreateUserToken(ctx context.Context, req *CreateUserTokenRequest) (string, error) {
|
|
|
|
var userID string
|
|
|
|
userIDVal := ctx.Value("userid")
|
|
|
|
if userIDVal != nil {
|
|
|
|
userID = userIDVal.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
isAdmin := false
|
|
|
|
isAdminVal := ctx.Value("admin")
|
|
|
|
if isAdminVal != nil {
|
|
|
|
isAdmin = isAdminVal.(bool)
|
|
|
|
}
|
|
|
|
|
2019-05-03 09:07:53 +00:00
|
|
|
userRef := req.UserRef
|
|
|
|
user, resp, err := c.configstoreClient.GetUser(ctx, userRef)
|
2019-04-05 13:56:06 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return "", ErrFromRemote(resp, errors.Wrapf(err, "failed to get user"))
|
2019-04-05 13:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// only admin or the same logged user can create a token
|
|
|
|
if !isAdmin && user.ID != userID {
|
|
|
|
return "", util.NewErrBadRequest(errors.Errorf("logged in user cannot create token for another user"))
|
|
|
|
}
|
|
|
|
if _, ok := user.Tokens[req.TokenName]; ok {
|
2019-05-03 09:07:53 +00:00
|
|
|
return "", util.NewErrBadRequest(errors.Errorf("user %q already have a token with name %q", userRef, req.TokenName))
|
2019-04-05 13:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.log.Infof("creating user token")
|
|
|
|
creq := &csapi.CreateUserTokenRequest{
|
|
|
|
TokenName: req.TokenName,
|
|
|
|
}
|
2019-05-03 09:07:53 +00:00
|
|
|
res, resp, err := c.configstoreClient.CreateUserToken(ctx, userRef, creq)
|
2019-04-05 13:56:06 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return "", ErrFromRemote(resp, errors.Wrapf(err, "failed to create user token"))
|
2019-04-05 13:56:06 +00:00
|
|
|
}
|
2019-05-03 09:07:53 +00:00
|
|
|
c.log.Infof("token %q for user %q created", req.TokenName, userRef)
|
2019-04-05 13:56:06 +00:00
|
|
|
|
|
|
|
return res.Token, nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
type CreateUserLARequest struct {
|
2019-05-03 09:07:53 +00:00
|
|
|
UserRef string
|
|
|
|
|
2019-04-29 12:57:07 +00:00
|
|
|
RemoteSourceName string
|
|
|
|
UserAccessToken string
|
|
|
|
Oauth2AccessToken string
|
|
|
|
Oauth2RefreshToken string
|
|
|
|
Oauth2AccessTokenExpiresAt time.Time
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommandHandler) CreateUserLA(ctx context.Context, req *CreateUserLARequest) (*types.LinkedAccount, error) {
|
2019-05-03 09:07:53 +00:00
|
|
|
userRef := req.UserRef
|
|
|
|
user, resp, err := c.configstoreClient.GetUser(ctx, userRef)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-05-03 09:07:53 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", userRef))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2019-05-03 09:07:53 +00:00
|
|
|
rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
c.log.Infof("rs: %s", util.Dump(rs))
|
|
|
|
var la *types.LinkedAccount
|
|
|
|
for _, v := range user.LinkedAccounts {
|
|
|
|
if v.RemoteSourceID == rs.ID {
|
|
|
|
la = v
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.log.Infof("la: %s", util.Dump(la))
|
|
|
|
if la != nil {
|
2019-05-03 09:07:53 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("user %q already have a linked account for remote source %q", userRef, rs.Name))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-29 13:42:10 +00:00
|
|
|
accessToken, err := common.GetAccessToken(rs, req.UserAccessToken, req.Oauth2AccessToken)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userSource, err := common.GetUserSource(rs, accessToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteUserInfo, err := userSource.GetUserInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to retrieve remote user info for remote source %q", rs.ID)
|
|
|
|
}
|
|
|
|
if remoteUserInfo.ID == "" {
|
|
|
|
return nil, errors.Errorf("empty remote user id for remote source %q", rs.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
creq := &csapi.CreateUserLARequest{
|
2019-04-29 12:57:07 +00:00
|
|
|
RemoteSourceName: req.RemoteSourceName,
|
|
|
|
RemoteUserID: remoteUserInfo.ID,
|
|
|
|
RemoteUserName: remoteUserInfo.LoginName,
|
|
|
|
UserAccessToken: req.UserAccessToken,
|
|
|
|
Oauth2AccessToken: req.Oauth2AccessToken,
|
|
|
|
Oauth2RefreshToken: req.Oauth2RefreshToken,
|
|
|
|
Oauth2AccessTokenExpiresAt: req.Oauth2AccessTokenExpiresAt,
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.log.Infof("creating linked account")
|
2019-05-03 09:07:53 +00:00
|
|
|
la, resp, err = c.configstoreClient.CreateUserLA(ctx, userRef, creq)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create linked account"))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2019-05-03 09:07:53 +00:00
|
|
|
c.log.Infof("linked account %q for user %q created", la.ID, userRef)
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
return la, nil
|
|
|
|
}
|
|
|
|
|
2019-05-03 09:07:53 +00:00
|
|
|
func (c *CommandHandler) UpdateUserLA(ctx context.Context, userRef string, la *types.LinkedAccount) error {
|
|
|
|
user, resp, err := c.configstoreClient.GetUser(ctx, userRef)
|
2019-04-29 13:42:10 +00:00
|
|
|
if err != nil {
|
2019-05-03 09:07:53 +00:00
|
|
|
return ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", userRef))
|
2019-04-29 13:42:10 +00:00
|
|
|
}
|
|
|
|
laFound := false
|
|
|
|
for _, ula := range user.LinkedAccounts {
|
|
|
|
if ula.ID == la.ID {
|
|
|
|
laFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.log.Infof("la: %s", util.Dump(la))
|
|
|
|
if !laFound {
|
2019-05-03 09:07:53 +00:00
|
|
|
return util.NewErrBadRequest(errors.Errorf("user %q doesn't have a linked account with id %q", userRef, la.ID))
|
2019-04-29 13:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
creq := &csapi.UpdateUserLARequest{
|
|
|
|
RemoteUserID: la.RemoteUserID,
|
|
|
|
RemoteUserName: la.RemoteUserName,
|
|
|
|
UserAccessToken: la.UserAccessToken,
|
|
|
|
Oauth2AccessToken: la.Oauth2AccessToken,
|
|
|
|
Oauth2RefreshToken: la.Oauth2RefreshToken,
|
|
|
|
Oauth2AccessTokenExpiresAt: la.Oauth2AccessTokenExpiresAt,
|
|
|
|
}
|
|
|
|
|
2019-05-03 09:07:53 +00:00
|
|
|
c.log.Infof("updating user %q linked account", userRef)
|
|
|
|
la, resp, err = c.configstoreClient.UpdateUserLA(ctx, userRef, la.ID, creq)
|
2019-04-29 13:42:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return ErrFromRemote(resp, errors.Wrapf(err, "failed to update user"))
|
|
|
|
}
|
2019-05-03 09:07:53 +00:00
|
|
|
c.log.Infof("linked account %q for user %q updated", la.ID, userRef)
|
2019-04-29 13:42:10 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RefreshLinkedAccount refreshed the linked account oauth2 access token and update linked account in the configstore
|
|
|
|
func (c *CommandHandler) RefreshLinkedAccount(ctx context.Context, rs *types.RemoteSource, userName string, la *types.LinkedAccount) (*types.LinkedAccount, error) {
|
|
|
|
switch rs.AuthType {
|
|
|
|
case types.RemoteSourceAuthTypeOauth2:
|
|
|
|
// refresh access token if expired
|
|
|
|
if isAccessTokenExpired(la.Oauth2AccessTokenExpiresAt) {
|
|
|
|
userSource, err := common.GetOauth2Source(rs, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
token, err := userSource.RefreshOauth2Token(la.Oauth2RefreshToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if la.Oauth2AccessToken != token.AccessToken {
|
|
|
|
la.Oauth2AccessToken = token.AccessToken
|
|
|
|
la.Oauth2RefreshToken = token.RefreshToken
|
|
|
|
la.Oauth2AccessTokenExpiresAt = token.Expiry
|
|
|
|
|
|
|
|
if err := c.UpdateUserLA(ctx, userName, la); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to update linked account")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return la, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGitSource is a wrapper around common.GetGitSource that will also refresh
|
|
|
|
// the oauth2 access token and update the linked account when needed
|
|
|
|
func (c *CommandHandler) GetGitSource(ctx context.Context, rs *types.RemoteSource, userName string, la *types.LinkedAccount) (gitsource.GitSource, error) {
|
|
|
|
la, err := c.RefreshLinkedAccount(ctx, rs, userName, la)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return common.GetGitSource(rs, la)
|
|
|
|
}
|
|
|
|
|
2019-03-29 16:53:15 +00:00
|
|
|
type RegisterUserRequest struct {
|
2019-04-29 12:57:07 +00:00
|
|
|
UserName string
|
|
|
|
RemoteSourceName string
|
|
|
|
UserAccessToken string
|
|
|
|
Oauth2AccessToken string
|
|
|
|
Oauth2RefreshToken string
|
|
|
|
Oauth2AccessTokenExpiresAt time.Time
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommandHandler) RegisterUser(ctx context.Context, req *RegisterUserRequest) (*types.User, error) {
|
|
|
|
if req.UserName == "" {
|
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("user name required"))
|
|
|
|
}
|
|
|
|
if !util.ValidateName(req.UserName) {
|
2019-04-08 10:08:31 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("invalid user name %q", req.UserName))
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 09:07:53 +00:00
|
|
|
rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName)
|
2019-03-29 16:53:15 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName))
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
c.log.Infof("rs: %s", util.Dump(rs))
|
|
|
|
|
2019-04-29 13:42:10 +00:00
|
|
|
accessToken, err := common.GetAccessToken(rs, req.UserAccessToken, req.Oauth2AccessToken)
|
2019-03-29 16:53:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userSource, err := common.GetUserSource(rs, accessToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteUserInfo, err := userSource.GetUserInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to retrieve remote user info for remote source %q", rs.ID)
|
|
|
|
}
|
|
|
|
if remoteUserInfo.ID == "" {
|
|
|
|
return nil, errors.Errorf("empty remote user id for remote source %q", rs.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
creq := &csapi.CreateUserRequest{
|
|
|
|
UserName: req.UserName,
|
|
|
|
CreateUserLARequest: &csapi.CreateUserLARequest{
|
2019-04-29 12:57:07 +00:00
|
|
|
RemoteSourceName: req.RemoteSourceName,
|
|
|
|
RemoteUserID: remoteUserInfo.ID,
|
|
|
|
RemoteUserName: remoteUserInfo.LoginName,
|
|
|
|
UserAccessToken: req.UserAccessToken,
|
|
|
|
Oauth2AccessToken: req.Oauth2AccessToken,
|
|
|
|
Oauth2RefreshToken: req.Oauth2RefreshToken,
|
|
|
|
Oauth2AccessTokenExpiresAt: req.Oauth2AccessTokenExpiresAt,
|
2019-03-29 16:53:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c.log.Infof("creating user account")
|
2019-04-09 12:53:00 +00:00
|
|
|
u, resp, err := c.configstoreClient.CreateUser(ctx, creq)
|
2019-03-29 16:53:15 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to create linked account"))
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
c.log.Infof("user %q created", req.UserName)
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
type LoginUserRequest struct {
|
2019-04-29 12:57:07 +00:00
|
|
|
RemoteSourceName string
|
|
|
|
UserAccessToken string
|
|
|
|
Oauth2AccessToken string
|
|
|
|
Oauth2RefreshToken string
|
|
|
|
Oauth2AccessTokenExpiresAt time.Time
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2019-03-29 16:53:15 +00:00
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
type LoginUserResponse struct {
|
|
|
|
Token string
|
|
|
|
User *types.User
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommandHandler) LoginUser(ctx context.Context, req *LoginUserRequest) (*LoginUserResponse, error) {
|
2019-05-03 09:07:53 +00:00
|
|
|
rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
c.log.Infof("rs: %s", util.Dump(rs))
|
|
|
|
|
2019-04-29 13:42:10 +00:00
|
|
|
accessToken, err := common.GetAccessToken(rs, req.UserAccessToken, req.Oauth2AccessToken)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userSource, err := common.GetUserSource(rs, accessToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteUserInfo, err := userSource.GetUserInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to retrieve remote user info for remote source %q", rs.ID)
|
|
|
|
}
|
|
|
|
if remoteUserInfo.ID == "" {
|
|
|
|
return nil, errors.Errorf("empty remote user id for remote source %q", rs.ID)
|
|
|
|
}
|
|
|
|
|
2019-04-09 12:53:00 +00:00
|
|
|
user, resp, err := c.configstoreClient.GetUserByLinkedAccountRemoteUserAndSource(ctx, remoteUserInfo.ID, rs.ID)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user for remote user id %q and remote source %q", remoteUserInfo.ID, rs.ID))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var la *types.LinkedAccount
|
|
|
|
for _, v := range user.LinkedAccounts {
|
|
|
|
if v.RemoteSourceID == rs.ID {
|
|
|
|
la = v
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.log.Infof("la: %s", util.Dump(la))
|
|
|
|
if la == nil {
|
2019-04-30 10:56:43 +00:00
|
|
|
return nil, errors.Errorf("linked account for user %q for remote source %q doesn't exist", user.Name, rs.Name)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update oauth tokens if they have changed since the getuserinfo request may have updated them
|
2019-04-29 12:57:07 +00:00
|
|
|
if la.Oauth2AccessToken != req.Oauth2AccessToken ||
|
|
|
|
la.Oauth2RefreshToken != req.Oauth2RefreshToken ||
|
|
|
|
la.UserAccessToken != req.UserAccessToken {
|
2019-02-21 16:58:25 +00:00
|
|
|
|
2019-04-29 12:57:07 +00:00
|
|
|
la.Oauth2AccessToken = req.Oauth2AccessToken
|
|
|
|
la.Oauth2RefreshToken = req.Oauth2RefreshToken
|
|
|
|
la.UserAccessToken = req.UserAccessToken
|
2019-02-21 16:58:25 +00:00
|
|
|
|
|
|
|
creq := &csapi.UpdateUserLARequest{
|
2019-04-29 12:57:07 +00:00
|
|
|
RemoteUserID: la.RemoteUserID,
|
|
|
|
RemoteUserName: la.RemoteUserName,
|
|
|
|
UserAccessToken: la.UserAccessToken,
|
|
|
|
Oauth2AccessToken: la.Oauth2AccessToken,
|
|
|
|
Oauth2RefreshToken: la.Oauth2RefreshToken,
|
|
|
|
Oauth2AccessTokenExpiresAt: la.Oauth2AccessTokenExpiresAt,
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 10:56:43 +00:00
|
|
|
c.log.Infof("updating user %q linked account", user.Name)
|
|
|
|
la, resp, err = c.configstoreClient.UpdateUserLA(ctx, user.Name, la.ID, creq)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to update user"))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2019-04-30 10:56:43 +00:00
|
|
|
c.log.Infof("linked account %q for user %q updated", la.ID, user.Name)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// generate jwt token
|
|
|
|
token, err := common.GenerateLoginJWTToken(c.sd, user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &LoginUserResponse{
|
|
|
|
Token: token,
|
|
|
|
User: user,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-29 16:53:15 +00:00
|
|
|
type AuthorizeRequest struct {
|
2019-04-29 12:57:07 +00:00
|
|
|
RemoteSourceName string
|
|
|
|
UserAccessToken string
|
|
|
|
Oauth2AccessToken string
|
|
|
|
Oauth2RefreshToken string
|
|
|
|
Oauth2AccessTokenExpiresAt time.Time
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AuthorizeResponse struct {
|
|
|
|
RemoteUserInfo *gitsource.UserInfo
|
|
|
|
RemoteSourceName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommandHandler) Authorize(ctx context.Context, req *AuthorizeRequest) (*AuthorizeResponse, error) {
|
2019-05-03 09:07:53 +00:00
|
|
|
rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, req.RemoteSourceName)
|
2019-03-29 16:53:15 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", req.RemoteSourceName))
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
|
|
|
c.log.Infof("rs: %s", util.Dump(rs))
|
|
|
|
|
2019-04-29 13:42:10 +00:00
|
|
|
accessToken, err := common.GetAccessToken(rs, req.UserAccessToken, req.Oauth2AccessToken)
|
2019-03-29 16:53:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userSource, err := common.GetUserSource(rs, accessToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteUserInfo, err := userSource.GetUserInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to retrieve remote user info for remote source %q", rs.ID)
|
|
|
|
}
|
|
|
|
if remoteUserInfo.ID == "" {
|
|
|
|
return nil, errors.Errorf("empty remote user id for remote source %q", rs.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &AuthorizeResponse{
|
|
|
|
RemoteUserInfo: remoteUserInfo,
|
|
|
|
RemoteSourceName: req.RemoteSourceName,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
type RemoteSourceAuthResponse struct {
|
|
|
|
Oauth2Redirect string
|
|
|
|
Response interface{}
|
|
|
|
}
|
|
|
|
|
2019-03-29 16:53:15 +00:00
|
|
|
func (c *CommandHandler) HandleRemoteSourceAuth(ctx context.Context, remoteSourceName, loginName, loginPassword string, requestType RemoteSourceRequestType, req interface{}) (*RemoteSourceAuthResponse, error) {
|
2019-05-03 09:07:53 +00:00
|
|
|
rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, remoteSourceName)
|
2019-03-29 13:33:41 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", remoteSourceName))
|
2019-03-29 13:33:41 +00:00
|
|
|
}
|
|
|
|
c.log.Infof("rs: %s", util.Dump(rs))
|
|
|
|
|
|
|
|
switch requestType {
|
2019-03-29 16:53:15 +00:00
|
|
|
case RemoteSourceRequestTypeCreateUserLA:
|
2019-03-29 13:33:41 +00:00
|
|
|
req := req.(*CreateUserLARequest)
|
2019-05-03 09:07:53 +00:00
|
|
|
user, resp, err := c.configstoreClient.GetUser(ctx, req.UserRef)
|
2019-03-29 13:33:41 +00:00
|
|
|
if err != nil {
|
2019-05-03 09:07:53 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get user %q", req.UserRef))
|
2019-03-29 13:33:41 +00:00
|
|
|
}
|
|
|
|
var la *types.LinkedAccount
|
|
|
|
for _, v := range user.LinkedAccounts {
|
|
|
|
if v.RemoteSourceID == rs.ID {
|
|
|
|
la = v
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.log.Infof("la: %s", util.Dump(la))
|
|
|
|
if la != nil {
|
2019-05-03 09:07:53 +00:00
|
|
|
return nil, util.NewErrBadRequest(errors.Errorf("user %q already have a linked account for remote source %q", req.UserRef, rs.Name))
|
2019-03-29 13:33:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 16:53:15 +00:00
|
|
|
case RemoteSourceRequestTypeLoginUser:
|
|
|
|
|
|
|
|
case RemoteSourceRequestTypeAuthorize:
|
|
|
|
|
|
|
|
case RemoteSourceRequestTypeRegisterUser:
|
2019-03-29 13:33:41 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unknown request type: %q", requestType)
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
switch rs.AuthType {
|
|
|
|
case types.RemoteSourceAuthTypeOauth2:
|
|
|
|
oauth2Source, err := common.GetOauth2Source(rs, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to create git source")
|
|
|
|
}
|
2019-03-29 16:53:15 +00:00
|
|
|
token, err := common.GenerateJWTToken(c.sd, rs.Name, string(requestType), req)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
redirect, err := oauth2Source.GetOauth2AuthorizationURL(c.webExposedURL+"/oauth2/callback", token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.log.Infof("oauth2 redirect: %s", redirect)
|
|
|
|
|
|
|
|
return &RemoteSourceAuthResponse{
|
|
|
|
Oauth2Redirect: redirect,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
case types.RemoteSourceAuthTypePassword:
|
|
|
|
passwordSource, err := common.GetPasswordSource(rs, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to create git source")
|
|
|
|
}
|
2019-04-30 10:13:12 +00:00
|
|
|
tokenName := "agola-" + c.agolaID
|
|
|
|
accessToken, err := passwordSource.LoginPassword(loginName, loginPassword, tokenName)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to login to remote source %q with login name %q", rs.Name, loginName)
|
|
|
|
}
|
|
|
|
c.log.Infof("access token: %s", accessToken)
|
|
|
|
requestj, err := json.Marshal(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-29 12:57:07 +00:00
|
|
|
cres, err := c.HandleRemoteSourceAuthRequest(ctx, requestType, string(requestj), accessToken, "", "", time.Time{})
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &RemoteSourceAuthResponse{
|
|
|
|
Response: cres.Response,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unknown remote source authentication type: %q", rs.AuthType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 16:53:15 +00:00
|
|
|
type RemoteSourceRequestType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
RemoteSourceRequestTypeCreateUserLA RemoteSourceRequestType = "createuserla"
|
|
|
|
RemoteSourceRequestTypeLoginUser RemoteSourceRequestType = "loginuser"
|
|
|
|
RemoteSourceRequestTypeAuthorize RemoteSourceRequestType = "authorize"
|
|
|
|
RemoteSourceRequestTypeRegisterUser RemoteSourceRequestType = "registeruser"
|
|
|
|
)
|
|
|
|
|
2019-02-21 16:58:25 +00:00
|
|
|
type RemoteSourceAuthResult struct {
|
2019-03-29 16:53:15 +00:00
|
|
|
RequestType RemoteSourceRequestType
|
2019-02-21 16:58:25 +00:00
|
|
|
Response interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateUserLAResponse struct {
|
|
|
|
LinkedAccount *types.LinkedAccount
|
|
|
|
}
|
|
|
|
|
2019-04-29 12:57:07 +00:00
|
|
|
func (c *CommandHandler) HandleRemoteSourceAuthRequest(ctx context.Context, requestType RemoteSourceRequestType, requestString string, userAccessToken, oauth2AccessToken, oauth2RefreshToken string, oauth2AccessTokenExpiresAt time.Time) (*RemoteSourceAuthResult, error) {
|
2019-02-21 16:58:25 +00:00
|
|
|
switch requestType {
|
2019-03-29 16:53:15 +00:00
|
|
|
case RemoteSourceRequestTypeCreateUserLA:
|
2019-02-21 16:58:25 +00:00
|
|
|
var req *CreateUserLARequest
|
|
|
|
if err := json.Unmarshal([]byte(requestString), &req); err != nil {
|
|
|
|
return nil, errors.Errorf("failed to unmarshal request")
|
|
|
|
}
|
|
|
|
|
|
|
|
creq := &CreateUserLARequest{
|
2019-05-03 09:07:53 +00:00
|
|
|
UserRef: req.UserRef,
|
2019-04-29 12:57:07 +00:00
|
|
|
RemoteSourceName: req.RemoteSourceName,
|
|
|
|
UserAccessToken: userAccessToken,
|
|
|
|
Oauth2AccessToken: oauth2AccessToken,
|
|
|
|
Oauth2RefreshToken: oauth2RefreshToken,
|
|
|
|
Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt,
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
la, err := c.CreateUserLA(ctx, creq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &RemoteSourceAuthResult{
|
|
|
|
RequestType: requestType,
|
|
|
|
Response: &CreateUserLAResponse{
|
|
|
|
LinkedAccount: la,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
|
2019-04-29 12:57:07 +00:00
|
|
|
case RemoteSourceRequestTypeRegisterUser:
|
|
|
|
var req *RegisterUserRequest
|
2019-02-21 16:58:25 +00:00
|
|
|
if err := json.Unmarshal([]byte(requestString), &req); err != nil {
|
|
|
|
return nil, errors.Errorf("failed to unmarshal request")
|
|
|
|
}
|
|
|
|
|
2019-04-29 12:57:07 +00:00
|
|
|
creq := &RegisterUserRequest{
|
|
|
|
UserName: req.UserName,
|
|
|
|
RemoteSourceName: req.RemoteSourceName,
|
|
|
|
UserAccessToken: userAccessToken,
|
|
|
|
Oauth2AccessToken: oauth2AccessToken,
|
|
|
|
Oauth2RefreshToken: oauth2RefreshToken,
|
|
|
|
Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt,
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
2019-04-29 12:57:07 +00:00
|
|
|
cresp, err := c.RegisterUser(ctx, creq)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &RemoteSourceAuthResult{
|
|
|
|
RequestType: requestType,
|
2019-03-29 16:53:15 +00:00
|
|
|
Response: cresp,
|
|
|
|
}, nil
|
|
|
|
|
2019-04-29 12:57:07 +00:00
|
|
|
case RemoteSourceRequestTypeLoginUser:
|
|
|
|
var req *LoginUserRequest
|
2019-03-29 16:53:15 +00:00
|
|
|
if err := json.Unmarshal([]byte(requestString), &req); err != nil {
|
|
|
|
return nil, errors.Errorf("failed to unmarshal request")
|
|
|
|
}
|
|
|
|
|
2019-04-29 12:57:07 +00:00
|
|
|
creq := &LoginUserRequest{
|
|
|
|
RemoteSourceName: req.RemoteSourceName,
|
|
|
|
UserAccessToken: userAccessToken,
|
|
|
|
Oauth2AccessToken: oauth2AccessToken,
|
|
|
|
Oauth2RefreshToken: oauth2RefreshToken,
|
|
|
|
Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt,
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
2019-04-29 12:57:07 +00:00
|
|
|
cresp, err := c.LoginUser(ctx, creq)
|
2019-03-29 16:53:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &RemoteSourceAuthResult{
|
|
|
|
RequestType: requestType,
|
|
|
|
Response: cresp,
|
|
|
|
}, nil
|
|
|
|
|
2019-04-29 12:57:07 +00:00
|
|
|
case RemoteSourceRequestTypeAuthorize:
|
|
|
|
var req *AuthorizeRequest
|
2019-03-29 16:53:15 +00:00
|
|
|
if err := json.Unmarshal([]byte(requestString), &req); err != nil {
|
|
|
|
return nil, errors.Errorf("failed to unmarshal request")
|
|
|
|
}
|
|
|
|
|
2019-04-29 12:57:07 +00:00
|
|
|
creq := &AuthorizeRequest{
|
|
|
|
RemoteSourceName: req.RemoteSourceName,
|
|
|
|
UserAccessToken: userAccessToken,
|
|
|
|
Oauth2AccessToken: oauth2AccessToken,
|
|
|
|
Oauth2RefreshToken: oauth2RefreshToken,
|
|
|
|
Oauth2AccessTokenExpiresAt: oauth2AccessTokenExpiresAt,
|
2019-03-29 16:53:15 +00:00
|
|
|
}
|
2019-04-29 12:57:07 +00:00
|
|
|
cresp, err := c.Authorize(ctx, creq)
|
2019-03-29 16:53:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &RemoteSourceAuthResult{
|
|
|
|
RequestType: requestType,
|
|
|
|
Response: cresp,
|
2019-02-21 16:58:25 +00:00
|
|
|
}, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unknown request")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommandHandler) HandleOauth2Callback(ctx context.Context, code, state string) (*RemoteSourceAuthResult, error) {
|
|
|
|
token, err := jwt.Parse(state, func(token *jwt.Token) (interface{}, error) {
|
|
|
|
if token.Method != c.sd.Method {
|
|
|
|
return nil, errors.Errorf("unexpected signing method: %v", token.Header["alg"])
|
|
|
|
}
|
|
|
|
var key interface{}
|
|
|
|
switch c.sd.Method {
|
|
|
|
case jwt.SigningMethodRS256:
|
|
|
|
key = c.sd.PrivateKey
|
|
|
|
case jwt.SigningMethodHS256:
|
|
|
|
key = c.sd.Key
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("unsupported signing method %q", c.sd.Method.Alg())
|
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to parse jwt")
|
|
|
|
}
|
|
|
|
if !token.Valid {
|
|
|
|
return nil, errors.Errorf("invalid token")
|
|
|
|
}
|
|
|
|
|
|
|
|
claims := token.Claims.(jwt.MapClaims)
|
|
|
|
remoteSourceName := claims["remote_source_name"].(string)
|
2019-03-29 16:53:15 +00:00
|
|
|
requestType := RemoteSourceRequestType(claims["request_type"].(string))
|
2019-02-21 16:58:25 +00:00
|
|
|
requestString := claims["request"].(string)
|
|
|
|
|
2019-05-03 09:07:53 +00:00
|
|
|
rs, resp, err := c.configstoreClient.GetRemoteSource(ctx, remoteSourceName)
|
2019-02-21 16:58:25 +00:00
|
|
|
if err != nil {
|
2019-04-09 12:53:00 +00:00
|
|
|
return nil, ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", remoteSourceName))
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|
|
|
|
c.log.Infof("rs: %s", util.Dump(rs))
|
|
|
|
|
|
|
|
oauth2Source, err := common.GetOauth2Source(rs, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to create gitlab source")
|
|
|
|
}
|
|
|
|
|
|
|
|
oauth2Token, err := oauth2Source.RequestOauth2Token(c.webExposedURL+"/oauth2/callback", code)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-29 12:57:07 +00:00
|
|
|
return c.HandleRemoteSourceAuthRequest(ctx, requestType, requestString, "", oauth2Token.AccessToken, oauth2Token.RefreshToken, oauth2Token.Expiry)
|
2019-02-21 16:58:25 +00:00
|
|
|
}
|