Added bead to pass authorisation credentials on in JS HTTP requests

Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/9e4b62d4
Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/9e4b62d4
Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/9e4b62d4

Branch: refs/heads/develop
Commit: 9e4b62d435e3f3166ce710e551c6e78100f88df6
Parents: 65320f9
Author: Justin Mclean <jmcl...@apache.org>
Authored: Sun Apr 16 15:41:15 2017 +1000
Committer: Justin Mclean <jmcl...@apache.org>
Committed: Sun Apr 16 15:41:15 2017 +1000

----------------------------------------------------------------------
 .../flex/net/beads/CORSCredentialsBead.as       | 79 ++++++++++++++++++++
 1 file changed, 79 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/9e4b62d4/frameworks/projects/Network/src/main/flex/org/apache/flex/net/beads/CORSCredentialsBead.as
----------------------------------------------------------------------
diff --git 
a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/beads/CORSCredentialsBead.as
 
b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/beads/CORSCredentialsBead.as
new file mode 100644
index 0000000..6a44c90
--- /dev/null
+++ 
b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/beads/CORSCredentialsBead.as
@@ -0,0 +1,79 @@
+package org.apache.flex.net.beads {
+import org.apache.flex.core.IBead;
+import org.apache.flex.core.IStrand;
+import org.apache.flex.events.Event;
+import org.apache.flex.events.IEventDispatcher;
+
+COMPILE::SWF
+public class CORSCredentialsBead {
+    public function CORSCredentialsBead(withCredentials:Boolean = false) {
+        trace("Only needed for JavaScript HTTP Server calls");
+    }
+}
+
+/**
+ *  Bead to allow passing on user authentication information in a 
XMLHttpRequest request.
+ *
+ *  If you don't use this bead any cross domain calls that require user 
authentication
+ *  (via say basic authentication or cookies) will fail.
+ *
+ *  @productversion FlexJS 0.8
+ */
+COMPILE::JS
+public class CORSCredentialsBead implements IBead {
+
+    public function CORSCredentialsBead(withCredentials:Boolean = false) {
+        this.withCredentials = withCredentials;
+    }
+
+    private var _strand:IStrand;
+
+    /**
+     *  Listen for a pre and post send event to modify if user credentials are 
passed.
+     *
+     *  @productversion FlexJS 0.8
+     */
+    public function set strand(value:IStrand):void {
+        _strand = value;
+
+        IEventDispatcher(_strand).addEventListener("preSend", preSendHandler);
+        IEventDispatcher(_strand).addEventListener("postSend", 
postSendHandler);
+    }
+
+    /**
+     *  Modify the HTTP request to pass credentials.
+     *
+     *  @productversion FlexJS 0.8
+     */
+    protected function preSendHandler(event:Event):void {
+        (event.target.element as XMLHttpRequest).withCredentials = 
withCredentials;
+    }
+
+    /**
+     *  Clean up event listeners.
+     *
+     *  @productversion FlexJS 0.8
+     */
+    protected function postSendHandler(event:Event):void {
+        IEventDispatcher(_strand).removeEventListener("preSend", 
preSendHandler);
+        IEventDispatcher(_strand).removeEventListener("postSend", 
preSendHandler);
+    }
+
+    private var _withCredentials:Boolean = false;
+
+    /**
+     *  Pass the user credentials or not.
+     *
+     *  @productversion FlexJS 0.8
+     */
+    public function get withCredentials():Boolean
+    {
+        return _withCredentials;
+    }
+
+    public function set withCredentials(value:Boolean):void
+    {
+        _withCredentials = value;
+    }
+}
+}

Reply via email to