Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hilmarf committed May 6, 2024
1 parent 81ea75a commit 49e2e2f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 51 deletions.
47 changes: 0 additions & 47 deletions pkg/contexts/credentials/builtin/npm/identity/identity.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package identity

import (
"fmt"
"net/http"
"path"

. "net/url"
Expand All @@ -12,7 +10,6 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/credentials/identity/hostpath"
"github.com/open-component-model/ocm/pkg/listformat"
"github.com/open-component-model/ocm/pkg/logging"
"github.com/open-component-model/ocm/pkg/npm"
)

const (
Expand Down Expand Up @@ -68,47 +65,3 @@ func GetCredentials(ctx cpi.ContextProvider, repoUrl string, pkgName string) com
}
return credentials.Properties()
}

// BearerToken retrieves the bearer token for the given repository URL and package name.
// Either it's setup in the credentials or it will login to the registry and retrieve it.
func BearerToken(ctx cpi.ContextProvider, repoUrl string, pkgName string) (string, error) {
// get credentials and TODO cache it
cred := GetCredentials(ctx, repoUrl, pkgName)
if cred == nil {
return "", fmt.Errorf("no credentials found for %s. Couldn't upload '%s'", repoUrl, pkgName)
}
log := logging.Context().Logger(REALM)
log.Debug("found credentials")

// check if token exists, if not login and retrieve token
token := cred[ATTR_TOKEN]
if token != "" {
log.Debug("token found, skipping login")
return token, nil
}

// use user+pass+mail from credentials to login and retrieve bearer token
username := cred[ATTR_USERNAME]
password := cred[ATTR_PASSWORD]
email := cred[ATTR_EMAIL]
if username == "" || password == "" || email == "" {
return "", fmt.Errorf("credentials for %s are invalid. Username, password or email missing! Couldn't upload '%s'", repoUrl, pkgName)
}
log = log.WithValues("user", username, "repo", repoUrl)
log.Debug("login")

// TODO: check different kinds of .npmrc content
return npm.Login(repoUrl, username, password, email)
}

// Authorize the given request with the bearer token for the given repository URL and package name.
// If the token is empty (login failed or credentials not found), it will not be set.
func Authorize(req *http.Request, ctx cpi.ContextProvider, repoUrl string, pkgName string) {
token, err := BearerToken(ctx, repoUrl, pkgName)
if err != nil {
log := logging.Context().Logger(REALM)
log.Debug("Couldn't authorize", "error", err.Error(), "repo", repoUrl, "package", pkgName)
} else if token != "" {
req.Header.Set("authorization", "Bearer "+token)
}
}
2 changes: 1 addition & 1 deletion pkg/contexts/ocm/accessmethods/npm/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import (
"github.com/open-component-model/ocm/pkg/blobaccess"
"github.com/open-component-model/ocm/pkg/common/accessio"
"github.com/open-component-model/ocm/pkg/common/accessobj"
npm "github.com/open-component-model/ocm/pkg/contexts/credentials/builtin/npm/identity"
"github.com/open-component-model/ocm/pkg/contexts/credentials/cpi"
"github.com/open-component-model/ocm/pkg/contexts/datacontext/attrs/vfsattr"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi/accspeccpi"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/iotools"
"github.com/open-component-model/ocm/pkg/mime"
"github.com/open-component-model/ocm/pkg/npm"
"github.com/open-component-model/ocm/pkg/runtime"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"net/http"
"net/url"

npmCredentials "github.com/open-component-model/ocm/pkg/contexts/credentials/builtin/npm/identity"
"github.com/open-component-model/ocm/pkg/contexts/ocm/accessmethods/npm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/logging"
"github.com/open-component-model/ocm/pkg/mime"
npmLogin "github.com/open-component-model/ocm/pkg/npm"
)

const BLOB_HANDLER_NAME = "ocm/npmPackage"
Expand Down Expand Up @@ -52,7 +52,7 @@ func (b *artifactHandler) StoreBlob(blob cpi.BlobAccess, _ string, _ string, _ c
}

// read package.json from tarball to get name, version, etc.
log := logging.Context().Logger(npmCredentials.REALM)
log := logging.Context().Logger(npmLogin.REALM)
log.Debug("reading package.json from tarball")
var pkg *Package
pkg, err = prepare(data)
Expand All @@ -64,7 +64,7 @@ func (b *artifactHandler) StoreBlob(blob cpi.BlobAccess, _ string, _ string, _ c
log = log.WithValues("package", pkg.Name, "version", pkg.Version)
log.Debug("identified")

token, err := npmCredentials.BearerToken(ctx.GetContext(), b.spec.Url, pkg.Name)
token, err := npmLogin.BearerToken(ctx.GetContext(), b.spec.Url, pkg.Name)
if err != nil {
// we assume, it's not possible to publish anonymous - without token
return nil, err
Expand Down
50 changes: 50 additions & 0 deletions pkg/npm/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ import (
"io"
"net/http"
"net/url"

"github.com/open-component-model/ocm/pkg/contexts/credentials/builtin/npm/identity"
"github.com/open-component-model/ocm/pkg/contexts/credentials/cpi"
"github.com/open-component-model/ocm/pkg/logging"
)

var REALM = identity.REALM

// Login to npm registry (URL) and retrieve bearer token.
func Login(registry string, username string, password string, email string) (string, error) {
data := map[string]interface{}{
Expand Down Expand Up @@ -47,3 +53,47 @@ func Login(registry string, username string, password string, email string) (str
}
return token.Token, nil
}

// BearerToken retrieves the bearer token for the given repository URL and package name.
// Either it's setup in the credentials or it will login to the registry and retrieve it.
func BearerToken(ctx cpi.ContextProvider, repoUrl string, pkgName string) (string, error) {
// get credentials and TODO cache it
cred := identity.GetCredentials(ctx, repoUrl, pkgName)
if cred == nil {
return "", fmt.Errorf("no credentials found for %s. Couldn't upload '%s'", repoUrl, pkgName)
}
log := logging.Context().Logger(identity.REALM)
log.Debug("found credentials")

// check if token exists, if not login and retrieve token
token := cred[identity.ATTR_TOKEN]
if token != "" {
log.Debug("token found, skipping login")
return token, nil
}

// use user+pass+mail from credentials to login and retrieve bearer token
username := cred[identity.ATTR_USERNAME]
password := cred[identity.ATTR_PASSWORD]
email := cred[identity.ATTR_EMAIL]
if username == "" || password == "" || email == "" {
return "", fmt.Errorf("credentials for %s are invalid. Username, password or email missing! Couldn't upload '%s'", repoUrl, pkgName)
}
log = log.WithValues("user", username, "repo", repoUrl)
log.Debug("login")

// TODO: check different kinds of .npmrc content
return Login(repoUrl, username, password, email)
}

// Authorize the given request with the bearer token for the given repository URL and package name.
// If the token is empty (login failed or credentials not found), it will not be set.
func Authorize(req *http.Request, ctx cpi.ContextProvider, repoUrl string, pkgName string) {
token, err := BearerToken(ctx, repoUrl, pkgName)
if err != nil {
log := logging.Context().Logger(identity.REALM)
log.Debug("Couldn't authorize", "error", err.Error(), "repo", repoUrl, "package", pkgName)
} else if token != "" {
req.Header.Set("authorization", "Bearer "+token)
}
}

0 comments on commit 49e2e2f

Please sign in to comment.