@@ -748,11 +748,11 @@ public function addSignature($endpoint, $data, $method = 'POST', $type = null, $
748
748
*/
749
749
public function checkMessage ()
750
750
{
751
- $ this -> ok = $ _SERVER ['REQUEST_METHOD ' ] === 'POST ' ;
752
- if (!$ this -> ok ) {
751
+ $ ok = $ _SERVER ['REQUEST_METHOD ' ] === 'POST ' ;
752
+ if (!$ ok ) {
753
753
$ this ->reason = 'LTI messages must use HTTP POST ' ;
754
754
} elseif (!empty ($ this ->jwt ) && !empty ($ this ->jwt ->hasJwt ())) {
755
- $ this -> ok = false ;
755
+ $ ok = false ;
756
756
if (is_null ($ this ->messageParameters ['oauth_consumer_key ' ]) || (strlen ($ this ->messageParameters ['oauth_consumer_key ' ]) <= 0 )) {
757
757
$ this ->reason = 'Missing iss claim ' ;
758
758
} elseif (empty ($ this ->jwt ->getClaim ('iat ' , '' ))) {
@@ -764,7 +764,7 @@ public function checkMessage()
764
764
} elseif (empty ($ this ->jwt ->getClaim ('nonce ' , '' ))) {
765
765
$ this ->reason = 'Missing nonce claim ' ;
766
766
} else {
767
- $ this -> ok = true ;
767
+ $ ok = true ;
768
768
}
769
769
}
770
770
// Set signature method from request
@@ -775,21 +775,21 @@ public function checkMessage()
775
775
}
776
776
}
777
777
// Check all required launch parameters
778
- if ($ this -> ok ) {
779
- $ this -> ok = isset ($ this ->messageParameters ['lti_message_type ' ]);
780
- if (!$ this -> ok ) {
778
+ if ($ ok ) {
779
+ $ ok = isset ($ this ->messageParameters ['lti_message_type ' ]);
780
+ if (!$ ok ) {
781
781
$ this ->reason = 'Missing lti_message_type parameter. ' ;
782
782
}
783
783
}
784
- if ($ this -> ok ) {
785
- $ this -> ok = isset ($ this ->messageParameters ['lti_version ' ]) && in_array ($ this ->messageParameters ['lti_version ' ],
784
+ if ($ ok ) {
785
+ $ ok = isset ($ this ->messageParameters ['lti_version ' ]) && in_array ($ this ->messageParameters ['lti_version ' ],
786
786
Util::$ LTI_VERSIONS );
787
- if (!$ this -> ok ) {
787
+ if (!$ ok ) {
788
788
$ this ->reason = 'Invalid or missing lti_version parameter. ' ;
789
789
}
790
790
}
791
791
792
- return $ this -> ok ;
792
+ return $ ok ;
793
793
}
794
794
795
795
/**
@@ -839,6 +839,9 @@ public function verifySignature()
839
839
$ method = new OAuth \OAuthSignatureMethod_HMAC_SHA1 ();
840
840
$ server ->add_signature_method ($ method );
841
841
$ request = OAuth \OAuthRequest::from_request ();
842
+ if (isset ($ request ->get_parameters ()['_new_window ' ]) && !isset ($ this ->messageParameters ['_new_window ' ])) {
843
+ $ request ->unset_parameter ('_new_window ' );
844
+ }
842
845
$ server ->verify_request ($ request );
843
846
$ ok = true ;
844
847
} catch (\Exception $ e ) {
@@ -884,9 +887,13 @@ public function verifySignature()
884
887
###
885
888
886
889
/**
887
- * Parse the message
890
+ * Parse the message.
891
+ *
892
+ * @param bool $strictMode True if full compliance with the LTI specification is required
893
+ * @param bool $disableCookieCheck True if no cookie check should be made
894
+ * @param bool $generateWarnings True if warning messages should be generated
888
895
*/
889
- private function parseMessage ()
896
+ private function parseMessage ($ strictMode , $ disableCookieCheck , $ generateWarnings )
890
897
{
891
898
if (is_null ($ this ->messageParameters )) {
892
899
$ this ->getRawParameters ();
@@ -943,16 +950,32 @@ private function parseMessage()
943
950
if (isset ($ this ->rawParameters ['id_token ' ])) {
944
951
$ this ->ok = !empty ($ this ->rawParameters ['state ' ]);
945
952
if ($ this ->ok ) {
946
- $ nonce = new PlatformNonce ($ this ->platform , $ this ->rawParameters ['state ' ]);
953
+ $ state = $ this ->rawParameters ['state ' ];
954
+ if (!$ disableCookieCheck ) {
955
+ $ parts = explode ('. ' , $ state );
956
+ if (empty ($ _COOKIE ) && !isset ($ _POST ['_new_window ' ])) { // Reopen in a new window
957
+ Util::setTestCookie ();
958
+ $ _POST ['_new_window ' ] = '' ;
959
+ echo Util::sendForm ($ _SERVER ['REQUEST_URI ' ], $ _POST , '_blank ' );
960
+ exit ;
961
+ } elseif (!empty (session_id ()) && (count ($ parts ) > 1 ) && (session_id () !== $ parts [1 ])) { // Reset to original session
962
+ session_abort ();
963
+ session_id ($ parts [1 ]);
964
+ session_start ();
965
+ $ this ->onResetSessionId ();
966
+ }
967
+ Util::setTestCookie (true );
968
+ }
969
+ $ nonce = new PlatformNonce ($ this ->platform , $ state );
947
970
$ this ->ok = $ nonce ->load ();
948
971
if (!$ this ->ok ) {
949
972
$ platform = Platform::fromPlatformId ($ iss , $ aud , null , $ this ->dataConnector );
950
- $ nonce = new PlatformNonce ($ platform , $ this -> rawParameters [ ' state ' ] );
973
+ $ nonce = new PlatformNonce ($ platform , $ state );
951
974
$ this ->ok = $ nonce ->load ();
952
975
}
953
976
if (!$ this ->ok ) {
954
977
$ platform = Platform::fromPlatformId ($ iss , null , null , $ this ->dataConnector );
955
- $ nonce = new PlatformNonce ($ platform , $ this -> rawParameters [ ' state ' ] );
978
+ $ nonce = new PlatformNonce ($ platform , $ state );
956
979
$ this ->ok = $ nonce ->load ();
957
980
}
958
981
if ($ this ->ok ) {
@@ -965,7 +988,7 @@ private function parseMessage()
965
988
$ this ->messageParameters = array ();
966
989
$ this ->messageParameters ['oauth_consumer_key ' ] = $ aud ;
967
990
$ this ->messageParameters ['oauth_signature_method ' ] = $ this ->jwt ->getHeader ('alg ' );
968
- $ this ->parseClaims ();
991
+ $ this ->parseClaims ($ strictMode , $ generateWarnings );
969
992
} else {
970
993
$ this ->reason = 'state parameter is invalid or missing ' ;
971
994
}
@@ -985,8 +1008,34 @@ private function parseMessage()
985
1008
$ this ->reason .= ": {$ this ->rawParameters ['error_description ' ]}" ;
986
1009
}
987
1010
} else { // OAuth
988
- if (isset ($ this ->rawParameters ['oauth_consumer_key ' ]) && ($ this instanceof Tool)) {
989
- $ this ->platform = Platform::fromConsumerKey ($ this ->rawParameters ['oauth_consumer_key ' ], $ this ->dataConnector );
1011
+ if ($ this instanceof Tool) {
1012
+ if (isset ($ this ->rawParameters ['oauth_consumer_key ' ])) {
1013
+ $ this ->platform = Platform::fromConsumerKey ($ this ->rawParameters ['oauth_consumer_key ' ], $ this ->dataConnector );
1014
+ }
1015
+ if (isset ($ this ->rawParameters ['tool_state ' ])) { // Relaunch?
1016
+ $ state = $ this ->rawParameters ['tool_state ' ];
1017
+ if (!$ disableCookieCheck ) {
1018
+ $ parts = explode ('. ' , $ state );
1019
+ if (empty ($ _COOKIE ) && !isset ($ _POST ['_new_window ' ])) { // Reopen in a new window
1020
+ Util::setTestCookie ();
1021
+ $ _POST ['_new_window ' ] = '' ;
1022
+ echo Util::sendForm ($ _SERVER ['REQUEST_URI ' ], $ _POST , '_blank ' );
1023
+ exit ;
1024
+ } elseif (!empty (session_id ()) && (count ($ parts ) > 1 ) && (session_id () !== $ parts [1 ])) { // Reset to original session
1025
+ session_abort ();
1026
+ session_id ($ parts [1 ]);
1027
+ session_start ();
1028
+ $ this ->onResetSessionId ();
1029
+ }
1030
+ unset($ this ->rawParameters ['_new_window ' ]);
1031
+ Util::setTestCookie (true );
1032
+ }
1033
+ $ nonce = new PlatformNonce ($ this ->platform , $ state );
1034
+ $ this ->ok = $ nonce ->load ();
1035
+ if (!$ this ->ok ) {
1036
+ $ this ->reason = "Invalid tool_state parameter: ' {$ state }' " ;
1037
+ }
1038
+ }
990
1039
}
991
1040
$ this ->messageParameters = $ this ->rawParameters ;
992
1041
}
@@ -995,8 +1044,11 @@ private function parseMessage()
995
1044
996
1045
/**
997
1046
* Parse the claims
1047
+ *
1048
+ * @param bool $strictMode True if full compliance with the LTI specification is required
1049
+ * @param bool $generateWarnings True if warning messages should be generated
998
1050
*/
999
- private function parseClaims ()
1051
+ private function parseClaims ($ strictMode , $ generateWarnings )
1000
1052
{
1001
1053
$ payload = Util::cloneObject ($ this ->jwt ->getPayload ());
1002
1054
$ errors = array ();
@@ -1041,6 +1093,13 @@ private function parseClaims()
1041
1093
$ value = $ value ? 'true ' : 'false ' ;
1042
1094
} elseif (isset ($ mapping ['isInteger ' ]) && $ mapping ['isInteger ' ]) {
1043
1095
$ value = strval ($ value );
1096
+ } elseif (!is_string ($ value )) {
1097
+ if ($ generateWarnings ) {
1098
+ $ this ->warnings [] = "Value of claim ' {$ claim }' is not a string: ' {$ value }' " ;
1099
+ }
1100
+ if (!$ strictMode ) {
1101
+ $ value = strval ($ value );
1102
+ }
1044
1103
}
1045
1104
}
1046
1105
if (!is_null ($ value ) && is_string ($ value )) {
0 commit comments