token.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Keyring connection tokens all look the same, although they may have varying
  4. * amounts of information stuffed in their meta values. Store a meta value
  5. * called "_classname" which contains the name of a Keyring_Service class to
  6. * use to "re-hydrate" the service this token is associated with.
  7. * @see Keyring_Request_Token
  8. * @see Keyring_Access_Token
  9. *
  10. * @package Keyring
  11. */
  12. class Keyring_Token {
  13. var $name = false;
  14. var $token = false;
  15. var $meta = array();
  16. var $service = false; // Will contain a Keyring_Service object
  17. var $unique_id = false;
  18. /**
  19. * Create a Keyring_Token instance.
  20. * @param string $service Shortname for the service this token is for
  21. * @param mixed $token The actual auth token (OAuth, string, etc)
  22. * @param array $meta Additional information related to this token
  23. * @param mixed $uniq A unique identifier for this token (if available)
  24. */
  25. function __construct( $service, $token, $meta = array(), $uniq = false ) {
  26. $this->name = strtolower( $service ); // Name of the service this token is for
  27. $this->token = $token;
  28. $this->unique_id = $uniq;
  29. $this->meta = $meta;
  30. $this->get_service();
  31. }
  32. function __toString() {
  33. return (string) $this->token;
  34. }
  35. function get_uniq_id() {
  36. if ( isset( $this->unique_id ) )
  37. return $this->unique_id;
  38. return null;
  39. }
  40. function get_display() {
  41. if ( $service = $this->get_service() )
  42. return $service->get_display( $this );
  43. return $this->name;
  44. }
  45. function get_service() {
  46. if ( !$this->service ) {
  47. $class = $this->get_meta( '_classname', true );
  48. if ( $class && class_exists( $class ) ) {
  49. $this->service = call_user_func( array( $class, 'init' ) );
  50. } else {
  51. $this->service = Keyring::get_service_by_name( $this->get_name() );
  52. }
  53. }
  54. return $this->service;
  55. }
  56. function get_name() {
  57. return $this->name;
  58. }
  59. /**
  60. * Get a specific piece of meta data for this token, or all meta as an array.
  61. *
  62. * @param mixed $name The key name for a specific meta item, or false for all.
  63. * @param bool $allow_hidden Allow access to "hidden" meta (prefixed with "_")
  64. * @return Mixed meta value, array of meta values, or null
  65. */
  66. function get_meta( $name = false, $allow_hidden = false ) {
  67. $return = null;
  68. if ( $name ) {
  69. if ( '_' != substr( $name, 0, 1 ) || $allow_hidden ) {
  70. if ( isset( $this->meta[ $name ] ) ) {
  71. $return = $this->meta[ $name ];
  72. }
  73. }
  74. } else {
  75. foreach ( (array) $this->meta as $key => $val ) {
  76. if ( '_' != substr( $key, 0, 1 ) || $allow_hidden ) {
  77. $return[ $key ] = $val;
  78. }
  79. }
  80. }
  81. return $return;
  82. }
  83. /**
  84. * Check if a token has expired, or will expire in the next $window seconds
  85. **/
  86. function is_expired( $window = 0 ) {
  87. if ( !$expires = $this->get_meta( 'expires' ) )
  88. return false; // No expires value, assume it's a permanent token
  89. if ( '0000-00-00 00:00:00' == $expires )
  90. return false; // Doesn't expire
  91. if ( ( time() + $window ) > strtotime( $expires ) )
  92. return true; // Token's expiry time has passed, or will pass before $window
  93. // Not expired
  94. return false;
  95. }
  96. }
  97. /**
  98. * During the first phase of the auth flow, we normally want to (or are required to)
  99. * store some details before sending the user off to a remote service to grant access.
  100. * Use a request token to store those details locally, then we can retrieve them when
  101. * we get back to finish the auth flow.
  102. */
  103. class Keyring_Request_Token extends Keyring_Token {
  104. function __construct( $service, $token, $meta = array(), $uniq = false ) {
  105. $meta['type'] = 'request';
  106. return parent::__construct( $service, $token, $meta, $uniq );
  107. }
  108. function type() {
  109. return 'request';
  110. }
  111. }
  112. /**
  113. * Access tokens are what are 'permanently' stored, containing the information required
  114. * to make secure connections/requests on behalf of the user of a remote service.
  115. */
  116. class Keyring_Access_Token extends Keyring_Token {
  117. function __construct( $service, $token, $meta = array(), $uniq = false ) {
  118. $meta['type'] = 'access';
  119. return parent::__construct( $service, $token, $meta, $uniq );
  120. }
  121. function type() {
  122. return 'access';
  123. }
  124. }