Skip to content

Commit

Permalink
adding basic routings and login flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso committed Jun 14, 2024
1 parent e98c6f4 commit 43f8dfb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
45 changes: 32 additions & 13 deletions pkg/bitwarden/bitwarden.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,48 @@ limitations under the License.
package bitwarden

import (
"os"

"github.com/bitwarden/sdk-go"
)

func GetSecret() {
const (
defaultAPIURL = "https://api.bitwarden.com"
defaultIdentityURL = "https://identity.bitwarden.com"
defaultStatePath = ".bitwarden-state"
)

// RequestBase contains optional API_URL and IDENTITY_URL values. If not defined,
// defaults are used always.
type RequestBase struct {
APIURL string `yaml:"apiUrl,omitempty"`
IdentityURL string `yaml:"identityUrl,omitempty"`
}

// LoginRequest defines bitwarden login details to Secrets Manager.
type LoginRequest struct {
*RequestBase `yaml:",inline,omitempty"`

AccessToken string `yaml:"accessToken"`
StatePath string `yaml:"statePath,omitempty"`
}

// Login creates a session for further Bitwarden requests.
func Login(req *LoginRequest) error {
// Configuring the URLS is optional, set them to nil to use the default values
apiURL := os.Getenv("API_URL")
identityURL := os.Getenv("IDENTITY_URL")
apiURL := defaultAPIURL
identityURL := defaultIdentityURL

// TODO: Cache the client... or the session?
bitwardenClient, err := sdk.NewBitwardenClient(&apiURL, &identityURL)
if err != nil {
panic(err)
return err
}

defer bitwardenClient.Close()

accessToken := os.Getenv("ACCESS_TOKEN")
// Configuring the statePath is optional, pass nil
// in AccessTokenLogin() to not use state
statePath := os.Getenv("STATE_PATH")

if err := bitwardenClient.AccessTokenLogin(accessToken, &statePath); err != nil {
panic(err)
var statePath string
if req.StatePath == "" {
statePath = defaultStatePath
}

return bitwardenClient.AccessTokenLogin(req.AccessToken, &statePath)
}
31 changes: 17 additions & 14 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"

"github.com/external-secrets/bitwarden-sdk-server/pkg/bitwarden"
)

const (
Expand Down Expand Up @@ -58,18 +56,11 @@ func (s *Server) Run(_ context.Context) error {
r.Get("/live", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("live"))
})
r.Get(api+"/secret", func(w http.ResponseWriter, r *http.Request) {
bitwarden.GetSecret()
_, _ = w.Write([]byte("welcome"))
})
r.Delete(api+"/secret", func(w http.ResponseWriter, r *http.Request) {
bitwarden.GetSecret()
_, _ = w.Write([]byte("welcome"))
})
r.Post(api+"/secret", func(w http.ResponseWriter, r *http.Request) {
bitwarden.GetSecret()
_, _ = w.Write([]byte("welcome"))
})

r.Post(api+"/login", s.loginHandler)
r.Get(api+"/secret", s.getSecretHandler)
r.Delete(api+"/secret", s.deleteSecretHandler)
r.Post(api+"/secret", s.createSecretHandler)

srv := &http.Server{Addr: s.Addr, Handler: r, ReadTimeout: 5 * time.Second}
s.server = srv
Expand All @@ -85,3 +76,15 @@ func (s *Server) Run(_ context.Context) error {
func (s *Server) Shutdown(ctx context.Context) error {
return s.server.Shutdown(ctx)
}

func (s *Server) getSecretHandler(writer http.ResponseWriter, request *http.Request) {
}

func (s *Server) deleteSecretHandler(writer http.ResponseWriter, request *http.Request) {
}

func (s *Server) createSecretHandler(writer http.ResponseWriter, request *http.Request) {
}

func (s *Server) loginHandler(writer http.ResponseWriter, request *http.Request) {
}

0 comments on commit 43f8dfb

Please sign in to comment.