Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stickeegreg committed Oct 16, 2019
0 parents commit 29bb3f4
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "stickee/laravel-sftp-dual-auth",
"description": "Stickee Laravel SFTP Dual Auth",
"version": "0.1",
"keywords": [
"sftp",
"storage"
],
"homepage": "https://www.stickee.co.uk",
"readme": "README.md",
"license": "proprietary",
"autoload": {
"psr-4": {
"Stickee\\LaravelSftpDualAuth\\": "src/"
}
},
"require": {
"php": "^7.2"
}
}
42 changes: 42 additions & 0 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Stickee Laravel SFTP Dual Auth

Provides a dual-authentication filesystem driver for [Laravel](https://laravel.com/).
The built-in sftp driver supports key-based login and password-based login, but not both at the same time. This driver provides that capability.

## Installation

Install with composer:

`composer require stickee/laravel-sftp-dual-auth`

If you don't use auto-discovery, add the SftpDualAuthServiceProvider to the providers array in *config/app.php*

```
Stickee\LaravelSftpDualAuth\SftpDualAuthServiceProvider::class,
```

## Usage

You can noew use *sftp_dual_auth* as a disk driver in *config/filesystems.php*

```
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'my_dual_auth' => [
'driver' => 'sftp_dual_auth',
'host' => env('MY_SFTP_HOST'),
'username' => env('MY_SFTP_USERNAME'),
'password' => env('MY_SFTP_PASSWORD'),
'privateKey' => env('MY_SFTP_PRIVATE_KEY'),
'root' => env('MY_SFTP_ROOT', ''),
],
]
```

See the [League Flysystem SFTP documentation](https://flysystem.thephpleague.com/docs/adapter/sftp/) ([GitHub](https://github.com/thephpleague/flysystem-sftp)) for information on the configuration options.
31 changes: 31 additions & 0 deletions src/SftpDualAuthAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Stickee\LaravelSftpDualAuth;

use Exception;
use League\Flysystem\Sftp\SftpAdapter;
use LogicException;

/**
* Class SftpDualAuthAdapter
*/
class SftpDualAuthAdapter extends SftpAdapter
{
/**
* Login.
*
* @throws Exception
* @throws LogicException
*/
protected function login()
{
if ($this->hostFingerprint) {
throw new Exception('Host fingerprint functionality not implemented');
}

if (!$this->connection->login($this->getUsername(), $this->getPrivateKey())
&& !$this->connection->login($this->getUsername(), $this->getPassword())) {
throw new LogicException('Could not login with username: ' . $this->getUsername() . ', host: ' . $this->host);
}
}
}
38 changes: 38 additions & 0 deletions src/SftpDualAuthServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Stickee\LaravelSftpDualAuth;

use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\Filesystem;
use Storage;

/**
* SftpDualAuthServiceProvider
*/
class SftpDualAuthServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
/**
* Create an instance of the sftp dual auth driver.
*
* @param \Illuminate\Foundation\Application $app The app container
* @param array $config The disk configuration
*
* @return \Illuminate\Contracts\Filesystem\Filesystem
*/
Storage::extend('sftp_dual_auth', function (Application $app, array $config) {
$adapter = new SftpDualAuthAdapter($config);
$filesystem = new Filesystem($adapter, count($config) > 0 ? $config : null);

return new FilesystemAdapter($filesystem);
});
}
}

0 comments on commit 29bb3f4

Please sign in to comment.