Skip to content

Commit

Permalink
moved npm.Login()
Browse files Browse the repository at this point in the history
  • Loading branch information
hilmarf committed May 3, 2024
1 parent 8a1b3b3 commit deb712a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
47 changes: 2 additions & 45 deletions pkg/contexts/credentials/builtin/npm/identity/identity.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package identity

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"path"

. "net/url"
Expand All @@ -17,6 +12,7 @@ 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 @@ -102,8 +98,7 @@ func BearerToken(ctx cpi.ContextProvider, repoUrl string, pkgName string) (strin
log.Debug("login")

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

// Authorize the given request with the bearer token for the given repository URL and package name.
Expand All @@ -117,41 +112,3 @@ func Authorize(req *http.Request, ctx cpi.ContextProvider, repoUrl string, pkgNa
req.Header.Set("authorization", "Bearer "+token)
}
}

// 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{}{
"_id": "org.couchdb.user:" + username,
"name": username,
"email": email,
"password": password,
"type": "user",
}
marshal, err := json.Marshal(data)
if err != nil {
return "", err
}
req, err := http.NewRequestWithContext(context.Background(), http.MethodPut, registry+"/-/user/org.couchdb.user:"+url.PathEscape(username), bytes.NewReader(marshal))
if err != nil {
return "", err
}
req.SetBasicAuth(username, password)
req.Header.Set("content-type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode >= http.StatusBadRequest {
all, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("%d, %s", resp.StatusCode, string(all))
}
var token struct {
Token string `json:"token"`
}
err = json.NewDecoder(resp.Body).Decode(&token)
if err != nil {
return "", err
}
return token.Token, nil
}
49 changes: 49 additions & 0 deletions pkg/npm/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package npm

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)

// 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{}{
"_id": "org.couchdb.user:" + username,
"name": username,
"email": email,
"password": password,
"type": "user",
}
marshal, err := json.Marshal(data)
if err != nil {
return "", err
}
req, err := http.NewRequestWithContext(context.Background(), http.MethodPut, registry+"/-/user/org.couchdb.user:"+url.PathEscape(username), bytes.NewReader(marshal))
if err != nil {
return "", err
}
req.SetBasicAuth(username, password)
req.Header.Set("content-type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode >= http.StatusBadRequest {
all, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("%d, %s", resp.StatusCode, string(all))
}
var token struct {
Token string `json:"token"`
}
err = json.NewDecoder(resp.Body).Decode(&token)
if err != nil {
return "", err
}
return token.Token, nil
}

0 comments on commit deb712a

Please sign in to comment.