Sanctum.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Laravel\Sanctum;
  3. use Mockery;
  4. class Sanctum
  5. {
  6. /**
  7. * The personal access client model class name.
  8. *
  9. * @var string
  10. */
  11. public static $personalAccessTokenModel = 'Laravel\\Sanctum\\PersonalAccessToken';
  12. /**
  13. * A callback that can get the token from the request.
  14. *
  15. * @var callable|null
  16. */
  17. public static $accessTokenRetrievalCallback;
  18. /**
  19. * A callback that can add to the validation of the access token.
  20. *
  21. * @var callable|null
  22. */
  23. public static $accessTokenAuthenticationCallback;
  24. /**
  25. * Indicates if Sanctum's migrations will be run.
  26. *
  27. * @var bool
  28. */
  29. public static $runsMigrations = true;
  30. /**
  31. * Get the current application URL from the "APP_URL" environment variable - with port.
  32. *
  33. * @return string
  34. */
  35. public static function currentApplicationUrlWithPort()
  36. {
  37. $appUrl = config('app.url');
  38. return $appUrl ? ','.parse_url($appUrl, PHP_URL_HOST).(parse_url($appUrl, PHP_URL_PORT) ? ':'.parse_url($appUrl, PHP_URL_PORT) : '') : '';
  39. }
  40. /**
  41. * Set the current user for the application with the given abilities.
  42. *
  43. * @param \Illuminate\Contracts\Auth\Authenticatable|\Laravel\Sanctum\HasApiTokens $user
  44. * @param array $abilities
  45. * @param string $guard
  46. * @return \Illuminate\Contracts\Auth\Authenticatable
  47. */
  48. public static function actingAs($user, $abilities = [], $guard = 'sanctum')
  49. {
  50. $token = Mockery::mock(self::personalAccessTokenModel())->shouldIgnoreMissing(false);
  51. if (in_array('*', $abilities)) {
  52. $token->shouldReceive('can')->withAnyArgs()->andReturn(true);
  53. } else {
  54. foreach ($abilities as $ability) {
  55. $token->shouldReceive('can')->with($ability)->andReturn(true);
  56. }
  57. }
  58. $user->withAccessToken($token);
  59. if (isset($user->wasRecentlyCreated) && $user->wasRecentlyCreated) {
  60. $user->wasRecentlyCreated = false;
  61. }
  62. app('auth')->guard($guard)->setUser($user);
  63. app('auth')->shouldUse($guard);
  64. return $user;
  65. }
  66. /**
  67. * Set the personal access token model name.
  68. *
  69. * @param string $model
  70. * @return void
  71. */
  72. public static function usePersonalAccessTokenModel($model)
  73. {
  74. static::$personalAccessTokenModel = $model;
  75. }
  76. /**
  77. * Specify a callback that should be used to fetch the access token from the request.
  78. *
  79. * @param callable $callback
  80. * @return void
  81. */
  82. public static function getAccessTokenFromRequestUsing(callable $callback)
  83. {
  84. static::$accessTokenRetrievalCallback = $callback;
  85. }
  86. /**
  87. * Specify a callback that should be used to authenticate access tokens.
  88. *
  89. * @param callable $callback
  90. * @return void
  91. */
  92. public static function authenticateAccessTokensUsing(callable $callback)
  93. {
  94. static::$accessTokenAuthenticationCallback = $callback;
  95. }
  96. /**
  97. * Determine if Sanctum's migrations should be run.
  98. *
  99. * @return bool
  100. */
  101. public static function shouldRunMigrations()
  102. {
  103. return static::$runsMigrations;
  104. }
  105. /**
  106. * Configure Sanctum to not register its migrations.
  107. *
  108. * @return static
  109. */
  110. public static function ignoreMigrations()
  111. {
  112. static::$runsMigrations = false;
  113. return new static;
  114. }
  115. /**
  116. * Get the token model class name.
  117. *
  118. * @return string
  119. */
  120. public static function personalAccessTokenModel()
  121. {
  122. return static::$personalAccessTokenModel;
  123. }
  124. }