Skip to content

Commit

Permalink
check for existing token and reuse it, instead of login
Browse files Browse the repository at this point in the history
  • Loading branch information
hilmarf committed Feb 16, 2024
1 parent 3982246 commit a804a5f
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions pkg/contexts/ocm/blobhandler/handlers/generic/npm/blobhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,34 @@ func (b *artifactHandler) StoreBlob(blob cpi.BlobAccess, _ string, _ string, _ c
log = log.WithValues("package", pkg.Name, "version", pkg.Version)
log.Debug("identified")

// use user+pass+mail from credentials to login and retrieve bearer token
// get credentials
cred := npmCredentials.GetCredentials(ctx.GetContext(), b.spec.Url, pkg.Name)
username := cred[npmCredentials.ATTR_USERNAME]
password := cred[npmCredentials.ATTR_PASSWORD]
email := cred[npmCredentials.ATTR_EMAIL]
if username == "" || password == "" || email == "" {
return nil, fmt.Errorf("username, password or email missing")
}
log = log.WithValues("user", username, "repo", b.spec.Url)
log.Debug("login")
token, err := login(b.spec.Url, username, password, email)
if err != nil {
return nil, err
if cred == nil {
return nil, fmt.Errorf("No credentials found for %s. Couldn't upload '%s'.", b.spec.Url, pkg.Name)
}
log.Debug("found credentials")

// check if token exists, if not login and retrieve token
token := cred[npmCredentials.ATTR_TOKEN]
if token == "" {
// use user+pass+mail from credentials to login and retrieve bearer token
username := cred[npmCredentials.ATTR_USERNAME]
password := cred[npmCredentials.ATTR_PASSWORD]
email := cred[npmCredentials.ATTR_EMAIL]
if username == "" || password == "" || email == "" {
return nil, fmt.Errorf("No credentials for %s are invalid. Username, password or email missing! Couldn't upload '%s'.", b.spec.Url, pkg.Name)
}
log = log.WithValues("user", username, "repo", b.spec.Url)
log.Debug("login")

// TODO: check different kinds of .npmrc content
token, err = login(b.spec.Url, username, password, email)
if err != nil {
return nil, err
}
cred[npmCredentials.ATTR_TOKEN] = token // Frage: @mandelsoft funktioniert das?
} else {
log.Debug("token found, skipping login")
}

// check if package exists
Expand Down

0 comments on commit a804a5f

Please sign in to comment.