@@ -37,40 +37,8 @@ public function __construct(array $values = array())
3737 */
3838 protected function initialize ()
3939 {
40- if (!isset ($ this ['prop.public_key ' ])) {
41- throw new ParameterException ('No public key defined ' );
42- }
43- if (!isset ($ this ['prop.private_key ' ])) {
44- throw new ParameterException ('No private key defined ' );
45- }
46- if (!isset ($ this ['prop.audience ' ])) {
47- throw new ParameterException ('No audience for claim defined ' );
48- }
49- if (!isset ($ this ['prop.issuer ' ])) {
50- throw new ParameterException ('No issuer for claim defined ' );
51- }
52-
53- if (isset ($ this ['prop.log_level ' ]) && is_array ($ this ['prop.log_level ' ])) {
54- $ level = $ this ['prop.log_level ' ];
55- } else {
56- $ level = null ;
57- }
58-
59- if (isset ($ this ['prop.log_file ' ])) {
60-
61- if (is_file ($ this ['prop.log_file ' ])) {
62- $ this ['prop.log_file ' ] = fopen ($ this ['prop.log_file ' ], 'a+ ' );
63- }
64-
65- if (!is_resource ($ this ['prop.log_file ' ])) {
66- throw new ParameterException ('Log file should be valid file or resource ' );
67- }
68-
69- $ this ['logger ' ] = new StreamLogger ($ this ['prop.log_file ' ], $ level );
70- } else {
71- $ this ['logger ' ] = new StreamLogger (fopen ('php://stdout ' , 'a+ ' ), $ level );
72- }
73-
40+ $ this ->validateOptions ();
41+ $ this ->initializeLogger ();
7442 $ this ->configureRoute ();
7543 }
7644
@@ -84,8 +52,11 @@ protected function configureRoute()
8452
8553 try {
8654 $ parameters = new Parameters ($ this );
87- $ token = new ClaimSet ($ this ['prop.audience ' ], $ parameters ->getAccount (), $ this ['prop.issuer ' ]);
88-
55+ $ token = new ClaimSet (
56+ $ this ['prop.audience ' ],
57+ $ parameters ->getAccount (),
58+ $ this ['prop.issuer ' ]
59+ );
8960 if (null !== $ scope = $ parameters ->getScope ()) {
9061 list ($ type , $ name , $ actions ) = explode (': ' , $ scope , 3 );
9162 $ token ->addAccess (new Access ($ type , $ name , explode (', ' , $ actions )));
@@ -101,22 +72,76 @@ protected function configureRoute()
10172 }
10273 }
10374
104- $ token = JWT ::encode ($ token ->getArrayCopy (), $ this ['prop.private_key ' ], 'RS256 ' , $ this ->getKid ());
105-
10675 return $ this ->json (
107- ['token ' => $ token ],
108- Response::HTTP_OK ,
109- ['Content-Type ' => 'application/json ' ]
76+ [
77+ 'token ' => JWT ::encode (
78+ $ token ->getArrayCopy (),
79+ $ this ['prop.private_key ' ],
80+ 'RS256 ' ,
81+ $ this ->getKid ()
82+ ),
83+ ],
84+ Response::HTTP_OK
11085 );
11186
112-
11387 } catch (InvalidAccessException $ e ) {
114- return new Response ('Invalid credentials ' , Response::HTTP_UNAUTHORIZED );
88+ return new Response (
89+ $ e ->getMessage (),
90+ Response::HTTP_UNAUTHORIZED
91+ );
92+ } catch (\Exception $ e ) {
93+ $ this ['logger ' ]->error (
94+ sprintf ('Exception thrown: %s @ %s(%s), ' , $ e ->getMessage (), $ e ->getFile (), $ e ->getLine ()));
95+ return new Response ($ e ->getMessage (), Response::HTTP_INTERNAL_SERVER_ERROR );
11596 }
116-
11797 });
11898 }
11999
100+ /**
101+ * validate the given options
102+ *
103+ * @throws ParameterException
104+ */
105+ protected function validateOptions ()
106+ {
107+ if (!isset ($ this ['prop.public_key ' ])) {
108+ throw new ParameterException ('No public key defined ' );
109+ }
110+ if (!isset ($ this ['prop.private_key ' ])) {
111+ throw new ParameterException ('No private key defined ' );
112+ }
113+ if (!isset ($ this ['prop.audience ' ])) {
114+ throw new ParameterException ('No audience for claim defined ' );
115+ }
116+ if (!isset ($ this ['prop.issuer ' ])) {
117+ throw new ParameterException ('No issuer for claim defined ' );
118+ }
119+ }
120+
121+ /**
122+ * Setup logger
123+ */
124+ protected function initializeLogger ()
125+ {
126+ $ levels = (isset ($ this ['prop.log_level ' ])) ? (array ) $ this ['prop.log_level ' ] : null ;
127+
128+ if (isset ($ this ['prop.log_file ' ])) {
129+
130+ if (is_file ($ this ['prop.log_file ' ])) {
131+ $ this ['prop.log_file ' ] = fopen ($ this ['prop.log_file ' ], 'a+ ' );
132+ }
133+
134+ if (!is_resource ($ this ['prop.log_file ' ])) {
135+ throw new ParameterException (sprintf ('Logger file should be valid file or resource, given "%s" ' , gettype ($ this ['prop.log_file ' ])));
136+ }
137+
138+ $ this ['logger ' ] = new StreamLogger ($ this ['prop.log_file ' ], $ levels );
139+ } else {
140+ $ this ['logger ' ] = new StreamLogger (fopen ('php://stdout ' , 'w ' ), $ levels );
141+ }
142+ }
143+
144+
120145
121146 /**
122147 * Create a kid from the public that the registry will
@@ -131,7 +156,8 @@ public function getKid()
131156 throw new InvalidAccessException ();
132157 }
133158 $ key = preg_replace ('/\n|\r/ ' , '' , $ m ['DATA ' ]);
134- return implode (': ' , array_slice (str_split (rtrim (Base32::encode (hash ('sha256 ' , base64_decode ($ key ), true )), '= ' ), 4 ), 0 , 12 ));
159+ $ key = array_slice (str_split (rtrim (Base32::encode (hash ('sha256 ' , base64_decode ($ key ), true )), '= ' ), 4 ), 0 , 12 );
160+ return implode (': ' , $ key );
135161 }
136162
137163}
0 commit comments