apereo / phpCAS

Apereo PHP CAS Client
https://apereo.github.io/phpCAS/
Apache License 2.0
796 stars 397 forks source link

Refactor CAS_Client, new public API? #62

Open adamfranco opened 11 years ago

adamfranco commented 11 years ago

The CAS_Client is currently a giant omnibus hunk of code weighing in at 3600 lines with 1123 statements (counted with grep -P ';|{' source/CAS/Client.php | wc -l). It has been discussed in other issues that the size and complexity of this class are problems. I'm opening this issue as a place to discuss options for refactoring the CAS_Client into smaller pieces that are easier to understand and work with.

My first stab at a refactoring the CAS_client was to pull all of the code used to proxy access to services (when using the CAS 2.0 protocol) into a separate CAS_Proxy class. This reduced the CAS_client to 2800 lines/862 statements. See the branch at https://github.com/Jasig/phpCAS/tree/Split_client_and_proxy for these changes. While these changes were certainly helpful, the client still has mess of different functions for each of the different protocols we support and complex switch or if/else statements to redirect the execution flow based on which protocol is in use.

Here's an initial proposal for refactoring the CAS_Client:

The big question: Does dividing up the client based on protocol seem like a reasonable option or can you think of a better way to slice it up?

leleuj commented 11 years ago

I don't know (much) the phpCAS client, though I hope my comment can help.

Refactoring the large phpCAS sources is a great idea, I think it makes sense to separate things according to the different versions of the protocol from a pure source code perspective and as the client configuration is done by protocol version.

The Java CAS client is "organized in a similar way". Nonetheless, there is something special about "proxy" I'd like to mention. When you talk about CAS_Client_Cas20Proxy, I understand that you mean requesting proxy granting tickets by using the pgtUrl parameter. I'm not sure to understand which class(es) will validate proxy tickets.

In the Java CAS client :

jfritschi commented 11 years ago

+1 for refactoring Cas_Client.

I always had this on my todo list for the last couple of years. I actually created a 2.0-future branch where i jotted down a first very rough outline on an idea. However i'm not sure if simple inheritance by protocol for the client is the right way to go. The SimpleCAS client from Brett uses another approach that has a really nice touch to it and it has pretty much stuck in my head. What i especially like is how a similar pattern might enable us to separate authentication code flow from protocol specific implementation details and also split out most of the helper/feature code (html, single sign-out, parsing etc.)

Your idea also makes sense but is not as modular and extendable as we should try to be in my view. I'm just thinking about the many features/protocols we have and i believe it makes sense to have them completely separate from the core workings of the client code. Especially thinking about #10 i think such a design would make more sense than a simple inheritance model.

So my rough idea would be to have a core client that uses different CAS protocol implementations that inherit protocols where possible and implement interfaces for specific advanced features (single sign out, proxy?) No idea if there is another pattern which we could use to achieve such an abstraction.

adamfranco commented 6 years ago

Given the direction of comments in this issue, this is probably the place to continue discussing what a new public API might look like as I think that will drive the direction of refactoring the CAS_Client.

Here is a first stab at one API design -- I'll try to think of a few more to give us ideas to ponder:

  1. Controller plus protocol implementation. Similar to the current static-method API, this would have clients interacting primarily with a controller instance, with protocol implementations getting instructions from the controller and handing their particular details.

Pseudo-code:

// General configuration is in controller.
$controller = new \PhpCas\Controller()
    // Optional injection of a custom protocol handler could be a class or object, usually not used.
    ->registerProtocolClient(CAS_VERSION_2_0_proxy, '\My\Impl') 
    // Normal configuration.
    ->useProtocol(CAS_VERSION_2_0_proxy)    
    ->setManageSessionId(true)
    ->setSessionHandler($mySessionHandler)
    ->setDebug('/var/log/phpcas.log');
// Protocol-specific configuration is in the client and will vary
// depending on the needs of the protocol
$controller->getProtocolClient()
    ->setHost('login.example.edu')
    ->setPort('443')
    ->setPath('/cas/')
    ->allowProxyChain(new \PhpCas\Protocol\Cas\ProxyChain(array(
        'https://app.example.com/'
     )));;

// Basic authentication flow interaction is against the controller.
$controller->forceAuthentication();
$userId = $controller->getUserId();
$attributes = $controller->getAttributes();
if ($_POST['logout'] && $controller->isAuthenticated()) {
    $controller->logout();
}

// Should protocol-specific functions (like proxy calls) be made against the controller
// or protocol client?
$request = $controller->getProtocolClient()
    ->getProxiedService(PHPCAS_PROXIED_SERVICE_HTTP_GET);
$request->setUrl('http://otherApp.example.com/something')
$request->send();
print $request->getBody();