gabrielcsapo / node-git-server

🎡 A configurable git server written in Node.js
https://gabrielcsapo.github.io/node-git-server
MIT License
253 stars 73 forks source link

Authenticate on clone #77

Closed asifashraf closed 2 years ago

asifashraf commented 3 years ago

The authentication example on readme looks good for push commands. But I can clone any random name and this will create a new repo automatically anonymously? If auto creating is doing that which is great, where is authentication in that?

aral commented 3 years ago

Just add a check for type === 'fetch' in the code that triggers your authentication:

const repositories = new Server(path.resolve(__dirname, 'tmp'), {
  autoCreate: true,
  authenticate: ({type, repo, user}, next) => {
    if (type === 'push' || type === 'fetch') {
      user((accountName, password) => {
        console.log('Authenticating:', accountName, password)
        if (accountName === '42' && password === '42') {
          next()
        } else {
          next('wrong password')
        }
      })
    } else {
      next()
    }
  }
})

(To figure it out, you would log the type and then perform a git clone.)

gabrielcsapo commented 2 years ago

Thanks for answering @aral, closing.