colymba / silverstripe-restfulapi

SilverStripe RESTful API with a default JSON serializer.
BSD 3-Clause "New" or "Revised" License
64 stars 34 forks source link

Getting error: Call to undefined method SilverStripe\Config\Collections\CachedConfigCollection::set() #98

Closed davidwieler closed 5 years ago

davidwieler commented 5 years ago

Hey @colymba, getting an error when trying to access the API with the token header X-Silverstripe-Apitoken. Without it, I get a token invalid error though.

Firstly, I had to change the Member model to be SilverStripe\Security\Member or it wouldn't add the token to the Member table when using the /api/auth/login, so maybe that has something to do with it.

If the token is in fact invalid, I get the proper error. It's only when the token is valid that this error happens.

I'm on SS 4.2.2

Config and Error trace is below.

Config:

# RestfulAPI config
Colymba\RESTfulAPI\RESTfulAPI:
  authentication_policy: true
  access_control_policy: 'ACL_CHECK_CONFIG_AND_MODEL'
  dependencies:
    authenticator: '%$Colymba\RESTfulAPI\Authenticators\TokenAuthenticator'
    authority: '%$Colymba\RESTfulAPI\PermissionManagers\DefaultPermissionManager'
    queryHandler: '%$Colymba\RESTfulAPI\QueryHandlers\DefaultQueryHandler'
  cors:
    Enabled: false
    Allow-Origin: 'localhost, http://mydomain.com'
    Allow-Headers: '*'
    Allow-Methods: 'OPTIONS, GET'
    Max-Age: 86400
  RESTfulAPI_TokenAuthenticator:
    tokenOwnerClass: 'SilverStripe\Security\Member'

Property:
  api_access: true
Page:
  api_access: true
SilverStripe\Security\Member:
  extensions:
    - Colymba\RESTfulAPI\Extensions\TokenAuthExtension

Error log:

Uncaught Error: Call to undefined method SilverStripe\Config\Collections\CachedConfigCollection::set()

GET /api/Property

Line 427 in /var/www/html/vendor/colymba/silverstripe-restfulapi/src/Authenticators/TokenAuthenticator.php

Source

418                 // check if token should automatically be updated
419                 if ($this->tokenConfig['autoRefresh']) {
420                     $tokenOwner->setField($this->tokenConfig['expireDBColumn'], $now + $life);
421                     $tokenOwner->write();
422                 }
423                 //all good, log Member in
424                 if (is_a($tokenOwner, Member::class)) {
425                     # $tokenOwner->logIn();
426                     # this is a login without the logging
427                     Config::inst()->set(Member::class, 'session_regenerate_id', true);
428                     $request->getSession()->set("loggedInAs", $tokenOwner->ID);
429                 }
430 
431                 return true;
432             } else {
433                 //too old
Trace

Colymba\RESTfulAPI\Authenticators\TokenAuthenticator->validateAPIToken(206b4a667a8319f0f56ebOpw8Kbmvh8TFuadejRh4udmcAJi4hRmy, SilverStripe\Control\HTTPRequest) 
TokenAuthenticator.php:379
Colymba\RESTfulAPI\Authenticators\TokenAuthenticator->authenticate(SilverStripe\Control\HTTPRequest) 
RESTfulAPI.php:322
Colymba\RESTfulAPI\RESTfulAPI->index(SilverStripe\Control\HTTPRequest) 
RequestHandler.php:320
SilverStripe\Control\RequestHandler->handleAction(SilverStripe\Control\HTTPRequest, index) 
Controller.php:284
SilverStripe\Control\Controller->handleAction(SilverStripe\Control\HTTPRequest, index) 
RequestHandler.php:202
SilverStripe\Control\RequestHandler->handleRequest(SilverStripe\Control\HTTPRequest) 
Controller.php:212
SilverStripe\Control\Controller->handleRequest(SilverStripe\Control\HTTPRequest) 
Director.php:361
SilverStripe\Control\Director->SilverStripe\Control\{closure}(SilverStripe\Control\HTTPRequest) 
VersionedHTTPMiddleware.php:41
SilverStripe\Versioned\VersionedHTTPMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
BasicAuthMiddleware.php:68
SilverStripe\Security\BasicAuthMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
AuthenticationMiddleware.php:61
SilverStripe\Security\AuthenticationMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
CanonicalURLMiddleware.php:188
SilverStripe\Control\Middleware\CanonicalURLMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
HTTPCacheControlMiddleware.php:42
SilverStripe\Control\Middleware\HTTPCacheControlMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
ChangeDetectionMiddleware.php:27
SilverStripe\Control\Middleware\ChangeDetectionMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
FlushMiddleware.php:29
SilverStripe\Control\Middleware\FlushMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
RequestProcessor.php:66
SilverStripe\Control\RequestProcessor->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
SessionMiddleware.php:20
SilverStripe\Control\Middleware\SessionMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
AllowedHostsMiddleware.php:60
SilverStripe\Control\Middleware\AllowedHostsMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
TrustedProxyMiddleware.php:176
SilverStripe\Control\Middleware\TrustedProxyMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
HTTPMiddlewareAware.php:65
SilverStripe\Control\Director->callMiddleware(SilverStripe\Control\HTTPRequest, Closure) 
Director.php:370
SilverStripe\Control\Director->handleRequest(SilverStripe\Control\HTTPRequest) 
HTTPApplication.php:48
SilverStripe\Control\HTTPApplication->SilverStripe\Control\{closure}(SilverStripe\Control\HTTPRequest) 
call_user_func(Closure, SilverStripe\Control\HTTPRequest) 
HTTPApplication.php:66
SilverStripe\Control\HTTPApplication->SilverStripe\Control\{closure}(SilverStripe\Control\HTTPRequest) 
call_user_func(Closure, SilverStripe\Control\HTTPRequest) 
ErrorControlChainMiddleware.php:73
SilverStripe\Core\Startup\ErrorControlChainMiddleware->SilverStripe\Core\Startup\{closure}(SilverStripe\Core\Startup\ErrorControlChain) 
call_user_func(Closure, SilverStripe\Core\Startup\ErrorControlChain) 
ErrorControlChain.php:235
SilverStripe\Core\Startup\ErrorControlChain->step() 
ErrorControlChain.php:225
SilverStripe\Core\Startup\ErrorControlChain->execute() 
ErrorControlChainMiddleware.php:92
SilverStripe\Core\Startup\ErrorControlChainMiddleware->process(SilverStripe\Control\HTTPRequest, Closure) 
HTTPMiddlewareAware.php:62
SilverStripe\Control\HTTPApplication->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest) 
HTTPMiddlewareAware.php:65
SilverStripe\Control\HTTPApplication->callMiddleware(SilverStripe\Control\HTTPRequest, Closure) 
HTTPApplication.php:67
SilverStripe\Control\HTTPApplication->execute(SilverStripe\Control\HTTPRequest, Closure, ) 
HTTPApplication.php:49
SilverStripe\Control\HTTPApplication->handle(SilverStripe\Control\HTTPRequest) 
index.php:25