Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init git access token for new user #142

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions user/component/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/bwmarrin/snowflake"
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
"github.com/google/uuid"
"opencsg.com/csghub-server/builder/git"
"opencsg.com/csghub-server/builder/git/gitserver"
"opencsg.com/csghub-server/builder/store/database"
Expand All @@ -20,11 +21,12 @@ import (
)

type UserComponent struct {
us *database.UserStore
os *database.OrgStore
ns *database.NamespaceStore
gs gitserver.GitServer
jwtc *JwtComponent
us *database.UserStore
os *database.OrgStore
ns *database.NamespaceStore
gs gitserver.GitServer
jwtc *JwtComponent
tokenc *AccessTokenComponent

casc *casdoorsdk.Client
casConfig *casdoorsdk.AuthConfig
Expand All @@ -39,6 +41,10 @@ func NewUserComponent(config *config.Config) (*UserComponent, error) {
c.os = database.NewOrgStore()
c.ns = database.NewNamespaceStore()
c.jwtc = NewJwtComponent(config.JWT.SigningKey, config.JWT.ValidHour)
c.tokenc, err = NewAccessTokenComponent(config)
if err != nil {
return nil, fmt.Errorf("fail to create access token component, error: %w", err)
}
c.gs, err = git.NewGitServer(config)
if err != nil {
newError := fmt.Errorf("failed to create git server,error:%w", err)
Expand Down Expand Up @@ -422,6 +428,19 @@ func (c *UserComponent) Signin(ctx context.Context, code, state string) (*types.
if err != nil {
return nil, "", fmt.Errorf("failed to create user,error:%w", err)
}
// auto create git access token for the new user
go func(username string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
_, err := c.tokenc.Create(ctx, &types.CreateUserTokenRequest{
Username: username,
TokenName: uuid.NewString(),
Application: types.AccessTokenAppGit,
})
if err != nil {
slog.Error("failed to create git user access token", "error", err, "username", dbu.Username)
}
}(dbu.Username)
} else {
// get user from db for username, as casdoor may have different username
dbu, err = c.us.FindByUUID(ctx, cu.Id)
Expand Down