Skip to content

Commit 0802037

Browse files
authored
Merge pull request #94 from codebar-ag/feature-direct
Feature Direct
2 parents 9fe558d + 017e3e5 commit 0802037

File tree

8 files changed

+62
-4
lines changed

8 files changed

+62
-4
lines changed

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -460,14 +460,20 @@ You only need to provide correct credentials. Everything else is automatically
460460
handled from the package. Under the hood we are storing the authentication
461461
cookie in the cache named *docuware.cookies*.
462462

463-
You can run `php artisan docuware:list-auth-cookie` command to get your auth session that you can use in your `.env` file `DOCUWARE_COOKIES` key.
463+
You can run `php artisan docuware:list-auth-cookie` command to get your auth session that you can use in your `.env`
464+
file `DOCUWARE_COOKIES` key.
464465

465466
But if you need further control you can use the following methods to login and
466467
logout with DocuWare:
467468

468469
```php
469470
use CodebarAg\DocuWare\Facades\DocuWare;
470471

472+
/**
473+
* If you would like to handle the cookie storage by yourself you can get a fresh cookie with the getCookie() method.
474+
*/
475+
DocuWare::getCookie();
476+
471477
/**
472478
* Login with your credentials. You only need to login once. Afterwards the
473479
* authentication cookie is stored in the cache as `docuware.cookies` and
@@ -566,6 +572,15 @@ This is the contents of the published config file:
566572
<?php
567573

568574
return [
575+
/*
576+
|--------------------------------------------------------------------------
577+
| Connection
578+
|--------------------------------------------------------------------------
579+
| Select a connector to authenticate with. You can choose between: WITHOUT_COOKIE, STATIC_COOKIE
580+
|
581+
*/
582+
583+
'connection' => ConnectionEnum::WITHOUT_COOKIE,
569584

570585
/*
571586
|--------------------------------------------------------------------------
@@ -698,6 +713,7 @@ cp phpunit.xml.dist phpunit.xml
698713
Modify environment variables in the phpunit.xml-file:
699714

700715
```xml
716+
701717
<env name="DOCUWARE_URL" value="https://domain.docuware.cloud"/>
702718
<env name="DOCUWARE_USERNAME" value="[email protected]"/>
703719
<env name="DOCUWARE_PASSWORD" value="password"/>

config/docuware.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
|--------------------------------------------------------------------------
99
| Connection
1010
|--------------------------------------------------------------------------
11-
| Select a connector to authenticate with. You can choose between: WITHOUT_COOKIE, STATIC_COOKIE, DYNAMIC_COOKIE
11+
| Select a connector to authenticate with. You can choose between: WITHOUT_COOKIE, STATIC_COOKIE
1212
|
1313
*/
1414

src/Console/ListAuthCookie.php renamed to src/Commands/ListAuthCookie.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace CodebarAg\DocuWare\Console;
3+
namespace CodebarAg\DocuWare\Commands;
44

55
use CodebarAg\DocuWare\Support\Auth;
66
use Illuminate\Console\Command;

src/DocuWare.php

+30
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,43 @@
5252

5353
class DocuWare
5454
{
55+
const COOKIE_NAME = '.DWPLATFORMAUTH';
56+
5557
protected $connection;
5658

5759
public function __construct(protected ?string $cookie = null)
5860
{
5961
$this->connection = self::connection();
6062
}
6163

64+
/**
65+
* @throws InvalidResponseClassException
66+
* @throws \Throwable
67+
* @throws \ReflectionException
68+
* @throws PendingRequestException
69+
*/
70+
public function getCookie(): string
71+
{
72+
EnsureValidCredentials::check();
73+
74+
$request = new PostLogonRequest();
75+
76+
$response = $this->connection->send($request);
77+
78+
event(new DocuWareResponseLog($response));
79+
80+
throw_if($response->status() === Response::HTTP_UNAUTHORIZED, UnableToLogin::create());
81+
throw_if($this->connection->getCoookieJar()->toArray() === [], UnableToLoginNoCookies::create());
82+
83+
$cookies = $this->connection->getCoookieJar();
84+
85+
$cookie = collect($cookies->toArray())
86+
->reject(fn (array $cookie) => Arr::get($cookie, 'Value') === '')
87+
->firstWhere('Name', self::COOKIE_NAME);
88+
89+
return Arr::get($cookie, 'Value');
90+
}
91+
6292
/**
6393
* @throws InvalidResponseClassException
6494
* @throws \Throwable

src/DocuWareServiceProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace CodebarAg\DocuWare;
44

5-
use CodebarAg\DocuWare\Console\ListAuthCookie;
5+
use CodebarAg\DocuWare\Commands\ListAuthCookie;
66
use Spatie\LaravelPackageTools\Package;
77
use Spatie\LaravelPackageTools\PackageServiceProvider;
88

src/Facades/DocuWare.php

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
/**
1919
* @see \CodebarAg\DocuWare\DocuWare
2020
*
21+
* @method static string getCookie()
2122
* @method static string login()
2223
* @method static void logout()
2324
* @method static Organization getOrganization(string $organizationId)

tests/Core/ArchTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
test('it will not use any debug function')
4+
->expect(['dd', 'ray', 'dump'])
5+
->not()
6+
->toBeUsed();

tests/Feature/AuthorizationTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
expect(Auth::cookies())->toBeNull();
1515
});
1616

17+
it('can receive a ', function () {
18+
$cookie = DocuWare::getCookie();
19+
expect($cookie)->not()->toBeEmpty()->toBeString($cookie);
20+
})->group('authorization');
21+
1722
it('can authenticate with a cookie', function () {
1823
EnsureValidCookie::check();
1924
expect(Auth::cookies())->toHaveKey(Auth::COOKIE_NAME);

0 commit comments

Comments
 (0)