2019-10-14 09:10:42 +03:00
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2019-10-14 09:10:42 +03:00
package externalaccount
import (
2023-09-25 16:17:37 +03:00
"context"
2024-06-20 16:24:53 +03:00
"strconv"
2019-10-14 09:10:42 +03:00
"strings"
2022-01-02 16:12:35 +03:00
"code.gitea.io/gitea/models/auth"
2023-09-09 00:09:23 +03:00
issues_model "code.gitea.io/gitea/models/issues"
repo_model "code.gitea.io/gitea/models/repo"
2021-11-24 12:49:20 +03:00
user_model "code.gitea.io/gitea/models/user"
2019-10-14 09:10:42 +03:00
"code.gitea.io/gitea/modules/structs"
"github.com/markbates/goth"
)
2023-10-14 11:37:24 +03:00
func toExternalLoginUser ( ctx context . Context , user * user_model . User , gothUser goth . User ) ( * user_model . ExternalLoginUser , error ) {
authSource , err := auth . GetActiveOAuth2SourceByName ( ctx , gothUser . Provider )
2019-10-14 09:10:42 +03:00
if err != nil {
2021-12-14 11:37:11 +03:00
return nil , err
2019-10-14 09:10:42 +03:00
}
2021-12-14 11:37:11 +03:00
return & user_model . ExternalLoginUser {
2019-10-14 09:10:42 +03:00
ExternalID : gothUser . UserID ,
UserID : user . ID ,
2022-01-02 16:12:35 +03:00
LoginSourceID : authSource . ID ,
2019-10-14 09:10:42 +03:00
RawData : gothUser . RawData ,
Provider : gothUser . Provider ,
Email : gothUser . Email ,
Name : gothUser . Name ,
FirstName : gothUser . FirstName ,
LastName : gothUser . LastName ,
NickName : gothUser . NickName ,
Description : gothUser . Description ,
AvatarURL : gothUser . AvatarURL ,
Location : gothUser . Location ,
AccessToken : gothUser . AccessToken ,
AccessTokenSecret : gothUser . AccessTokenSecret ,
RefreshToken : gothUser . RefreshToken ,
ExpiresAt : gothUser . ExpiresAt ,
2021-12-14 11:37:11 +03:00
} , nil
}
// LinkAccountToUser link the gothUser to the user
2023-09-25 16:17:37 +03:00
func LinkAccountToUser ( ctx context . Context , user * user_model . User , gothUser goth . User ) error {
2023-10-14 11:37:24 +03:00
externalLoginUser , err := toExternalLoginUser ( ctx , user , gothUser )
2021-12-14 11:37:11 +03:00
if err != nil {
return err
2019-10-14 09:10:42 +03:00
}
2023-10-14 11:37:24 +03:00
if err := user_model . LinkExternalToUser ( ctx , user , externalLoginUser ) ; err != nil {
2019-10-14 09:10:42 +03:00
return err
}
2019-10-17 05:06:28 +03:00
externalID := externalLoginUser . ExternalID
2019-10-14 09:10:42 +03:00
var tp structs . GitServiceType
for _ , s := range structs . SupportedFullGitService {
if strings . EqualFold ( s . Name ( ) , gothUser . Provider ) {
tp = s
break
}
}
if tp . Name ( ) != "" {
2023-09-25 16:17:37 +03:00
return UpdateMigrationsByType ( ctx , tp , externalID , user . ID )
2019-10-14 09:10:42 +03:00
}
return nil
}
2021-12-14 11:37:11 +03:00
allow synchronizing user status from OAuth2 login providers (#31572)
This leverages the existing `sync_external_users` cron job to
synchronize the `IsActive` flag on users who use an OAuth2 provider set
to synchronize. This synchronization is done by checking for expired
access tokens, and using the stored refresh token to request a new
access token. If the response back from the OAuth2 provider is the
`invalid_grant` error code, the user is marked as inactive. However, the
user is able to reactivate their account by logging in the web browser
through their OAuth2 flow.
Also changed to support this is that a linked `ExternalLoginUser` is
always created upon a login or signup via OAuth2.
Ideally, we would also refresh permissions from the configured OAuth
provider (e.g., admin, restricted and group mappings) to match the
implementation of LDAP. However, the OAuth library used for this `goth`,
doesn't seem to support issuing a session via refresh tokens. The
interface provides a [`RefreshToken`
method](https://github.com/markbates/goth/blob/master/provider.go#L20),
but the returned `oauth.Token` doesn't implement the `goth.Session` we
would need to call `FetchUser`. Due to specific implementations, we
would need to build a compatibility function for every provider, since
they cast to concrete types (e.g.
[Azure](https://github.com/markbates/goth/blob/master/providers/azureadv2/azureadv2.go#L132))
---------
Co-authored-by: Kyle D <kdumontnu@gmail.com>
(cherry picked from commit 416c36f3034e228a27258b5a8a15eec4e5e426ba)
Conflicts:
- tests/integration/auth_ldap_test.go
Trivial conflict resolved by manually applying the change.
- routers/web/auth/oauth.go
Technically not a conflict, but the original PR removed the
modules/util import, which in our version, is still in use. Added it
back.
2024-07-16 21:33:16 +03:00
// EnsureLinkExternalToUser link the gothUser to the user
func EnsureLinkExternalToUser ( ctx context . Context , user * user_model . User , gothUser goth . User ) error {
2023-10-14 11:37:24 +03:00
externalLoginUser , err := toExternalLoginUser ( ctx , user , gothUser )
2021-12-14 11:37:11 +03:00
if err != nil {
return err
}
allow synchronizing user status from OAuth2 login providers (#31572)
This leverages the existing `sync_external_users` cron job to
synchronize the `IsActive` flag on users who use an OAuth2 provider set
to synchronize. This synchronization is done by checking for expired
access tokens, and using the stored refresh token to request a new
access token. If the response back from the OAuth2 provider is the
`invalid_grant` error code, the user is marked as inactive. However, the
user is able to reactivate their account by logging in the web browser
through their OAuth2 flow.
Also changed to support this is that a linked `ExternalLoginUser` is
always created upon a login or signup via OAuth2.
Ideally, we would also refresh permissions from the configured OAuth
provider (e.g., admin, restricted and group mappings) to match the
implementation of LDAP. However, the OAuth library used for this `goth`,
doesn't seem to support issuing a session via refresh tokens. The
interface provides a [`RefreshToken`
method](https://github.com/markbates/goth/blob/master/provider.go#L20),
but the returned `oauth.Token` doesn't implement the `goth.Session` we
would need to call `FetchUser`. Due to specific implementations, we
would need to build a compatibility function for every provider, since
they cast to concrete types (e.g.
[Azure](https://github.com/markbates/goth/blob/master/providers/azureadv2/azureadv2.go#L132))
---------
Co-authored-by: Kyle D <kdumontnu@gmail.com>
(cherry picked from commit 416c36f3034e228a27258b5a8a15eec4e5e426ba)
Conflicts:
- tests/integration/auth_ldap_test.go
Trivial conflict resolved by manually applying the change.
- routers/web/auth/oauth.go
Technically not a conflict, but the original PR removed the
modules/util import, which in our version, is still in use. Added it
back.
2024-07-16 21:33:16 +03:00
return user_model . EnsureLinkExternalToUser ( ctx , externalLoginUser )
2021-12-14 11:37:11 +03:00
}
2023-09-09 00:09:23 +03:00
// UpdateMigrationsByType updates all migrated repositories' posterid from gitServiceType to replace originalAuthorID to posterID
2023-09-25 16:17:37 +03:00
func UpdateMigrationsByType ( ctx context . Context , tp structs . GitServiceType , externalUserID string , userID int64 ) error {
2024-06-20 16:24:53 +03:00
// Skip update if externalUserID is not a valid numeric ID or exceeds int64
if _ , err := strconv . ParseInt ( externalUserID , 10 , 64 ) ; err != nil {
return nil
}
2023-09-29 15:12:54 +03:00
if err := issues_model . UpdateIssuesMigrationsByType ( ctx , tp , externalUserID , userID ) ; err != nil {
2023-09-09 00:09:23 +03:00
return err
}
2023-09-29 15:12:54 +03:00
if err := issues_model . UpdateCommentsMigrationsByType ( ctx , tp , externalUserID , userID ) ; err != nil {
2023-09-09 00:09:23 +03:00
return err
}
2023-09-25 16:17:37 +03:00
if err := repo_model . UpdateReleasesMigrationsByType ( ctx , tp , externalUserID , userID ) ; err != nil {
2023-09-09 00:09:23 +03:00
return err
}
2023-09-29 15:12:54 +03:00
if err := issues_model . UpdateReactionsMigrationsByType ( ctx , tp , externalUserID , userID ) ; err != nil {
2023-09-09 00:09:23 +03:00
return err
}
2023-09-29 15:12:54 +03:00
return issues_model . UpdateReviewsMigrationsByType ( ctx , tp , externalUserID , userID )
2023-09-09 00:09:23 +03:00
}