1: <?php
2:
3: namespace PHPixie\AuthHTTP\Providers;
4:
5: class Cookie extends \PHPixie\Auth\Providers\Provider\Implementation
6: implements \PHPixie\Auth\Providers\Provider\Persistent
7: {
8: protected $tokens;
9: protected $httpContextContainer;
10: protected $cookieName;
11: protected $tokenHandler;
12:
13: public function __construct($tokens, $httpContextContainer, $domain, $name, $configData)
14: {
15: $this->tokens = $tokens;
16: $this->httpContextContainer = $httpContextContainer;
17:
18: parent::__construct($domain, $name, $configData);
19: }
20:
21: public function check()
22: {
23: $encodedToken = $this->getCookie();
24:
25: if($encodedToken === null) {
26: return null;
27: }
28:
29: $token = $this->tokenHandler()->getByString($encodedToken);
30:
31: if($token === null) {
32: $this->unsetCookie();
33: return null;
34: }
35:
36: $userId = $token->userId();
37: $user = $this->repository()->getById($userId);
38:
39: if($user === null) {
40: $this->removeToken($encodedToken);
41: $this->unsetCookie();
42: return null;
43: }
44:
45: if($this->configData->get('refresh', true)) {
46: $token = $this->tokenHandler->refresh($token);
47: $this->setCookie($token);
48: }
49:
50: $this->domain->setUser($user, $this->name);
51:
52: $persistProviders = $this->configData->get('persistProviders', array());
53:
54: foreach($persistProviders as $providerName) {
55: $this->domain->provider($providerName)->persist();
56: }
57:
58: return $user;
59: }
60:
61: public function persist($lifetime = null)
62: {
63: if($lifetime === null) {
64: $lifetime = $this->configData->get('defaultLifetime', 14*24*3600);
65: }
66:
67: $user = $this->domain->requireUser();
68: $token = $this->tokenHandler()->create($user->id(), $lifetime);
69: $this->setCookie($token);
70: }
71:
72: public function forget()
73: {
74: $encodedToken = $this->getCookie();
75:
76: if($encodedToken === null) {
77: return;
78: }
79:
80: $this->unsetCookie();
81: $this->removeToken($encodedToken);
82: }
83:
84: protected function setCookie($token)
85: {
86: $cookies = $this->cookies();
87: $cookies->set(
88: $this->cookieName(),
89: $token->string(),
90: $token->expires() - time(),
91: '/',
92: null,
93: false,
94: true
95: );
96: }
97:
98:
99: protected function getCookie()
100: {
101: $this->cookieName();
102: return $this->cookies()->get($this->cookieName);
103: }
104:
105: protected function unsetCookie()
106: {
107: $this->cookies()->remove($this->cookieName());
108: }
109:
110: protected function removeToken($encodedToken)
111: {
112: $this->tokenHandler()->removeByString($encodedToken);
113: }
114:
115: protected function cookieName()
116: {
117: if($this->cookieName === null) {
118: $defaultKey = $this->domain->name().'Token';
119: $this->cookieName = $this->configData->get('cookie', $defaultKey);
120: }
121:
122: return $this->cookieName;
123: }
124:
125: protected function tokenHandler()
126: {
127: if($this->tokenHandler === null) {
128: $configData = $this->configData->slice('tokens');
129: $this->tokenHandler = $this->tokens->handler($configData);
130: }
131:
132: return $this->tokenHandler;
133: }
134:
135: protected function cookies()
136: {
137: $httpContext = $this->httpContextContainer->httpContext();
138: return $httpContext->cookies();
139: }
140: }
141: