5a9285fcc496039e6130c9c84d47def1fb193028
[potlatch2.git] / net / systemeD / potlatch2 / save / OAuthPanel.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:TitleWindow
3         xmlns:mx="http://www.adobe.com/2006/mxml" 
4         layout="vertical"
5         horizontalAlign="center" title="Authorisation Required"
6         creationComplete="getRequestToken()"
7         height="290">
8         
9         <mx:ViewStack id="contentStack" width="100%" height="100%">
10         
11         <mx:VBox id="okPanel" width="100%" height="100%">
12           <mx:Text width="100%" text="{getAuthText()}"/>
13           <mx:VBox width="100%" id="gotLinkBox" visible="false">
14             <mx:Text width="100%">
15               <mx:text>
16                 Click the link below to open a web page where
17                 you will be asked to authorise access to this app.
18               </mx:text>
19             </mx:Text>
20             <mx:LinkButton id="link"
21                 label="http://oauth.dev.openstreetmap.org/oauth/authorize?somekey"
22                 click="openURL(authoriseURL); tryAccessButton.enabled=true;"/>
23             <mx:Text width="100%">
24               <mx:text>Once you've authorised the access click the 'Try Access' button below</mx:text> <!-- ' -->
25             </mx:Text>
26             <mx:Text styleName="failText" visible="false" id="deniedLabel">
27               <mx:text><![CDATA[<b>Access was denied, please check, and try again</b>]]></mx:text>
28             </mx:Text>
29             <mx:HBox width="100%" horizontalAlign="right">
30               <mx:CheckBox id="rememberMe" label="Remember authorisation" selected="true"/>
31             </mx:HBox>
32           </mx:VBox>
33         </mx:VBox>
34         
35         <mx:VBox id="permFailPanel" width="100%" height="100%">
36           <mx:Text styleName="failText" width="100%" condenseWhite="true">
37             <mx:htmlText><![CDATA[
38               <p>The server refused this application's credentials -- an authorisation link
39               could not be obtained.
40               </p><p>
41               <b>OAuth access will not be possible.</b>
42               </p><p>
43               Please contact application vendor to find out what's going on.
44             ]]></mx:htmlText>
45           </mx:Text>
46         </mx:VBox>
47         
48         <mx:VBox id="tempFailPanel" width="100%" height="100%">
49           <mx:Text width="100%">
50             <mx:text>
51               There was a problem contacting the server to get authorisation.
52               This may be a temporary error, try again later.
53             </mx:text>
54           </mx:Text>
55         </mx:VBox>
56         
57         </mx:ViewStack>
58         
59         <mx:ControlBar horizontalAlign="right">
60         
61             <mx:ProgressBar id="progress" label="Contacting server..." labelPlacement="top"
62                 indeterminate="true"/>
63         <mx:Spacer width="100%"/>
64
65             <mx:Button label="Cancel" click="PopUpManager.removePopUp(this);" styleName="titleWindowButton" />
66             <mx:Button id="tryAccessButton" label="Try Access" click="getAccessToken()" enabled="false" styleName="titleWindowButton" />
67         </mx:ControlBar>
68         
69         <mx:Script><![CDATA[
70                 import flash.display.InteractiveObject;
71         import flash.events.Event;
72         import flash.net.*;
73                 import flash.system.Capabilities;
74         import mx.managers.PopUpManager;
75         import net.systemeD.halcyon.connection.*;
76         import org.iotashan.oauth.*;
77                 import flash.external.ExternalInterface;
78
79         private var connection:Connection;
80         private var requestToken:OAuthToken;
81         private var _accessToken:OAuthToken;
82         private var authoriseURL:String;
83         private var lastHTTPStatus:int = 0;
84         
85         public static var ACCESS_TOKEN_EVENT:String = "gotAccessToken";
86         
87         private function getAuthText():String {
88             return "To save data you must authorise this application to edit "+
89                     connection.serverName + " on your behalf.";
90         }
91         
92                 public function setConnection(connection:Connection):void {
93                         this.connection=connection;
94                 }
95
96         private function openURL(url:String):void {
97                         if (ExternalInterface.available) {
98                                 var winH:int = 560;
99                                 var winW:int = 600;
100                                 var leftPos:int  = (Capabilities.screenResolutionX - winW) / 2;
101                                 var topPos:int = (Capabilities.screenResolutionY - winH) / 2;
102                                 ExternalInterface.call( "window.open", url,"oAuthWin","height=" + winH + ",width=" + winW +",top=" + topPos + ", left=" + leftPos +", toolbar=no,scrollbars=no,status=no,location=no,menubar=no,directories=no");
103                                 }               
104                         else
105                         {
106                                 var urlRequest:URLRequest = new URLRequest(url);
107                                 navigateToURL(urlRequest,"_blank");
108                         }
109                 }
110         
111         private function getRequestToken():void {
112             var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
113             var consumer:OAuthConsumer = getConsumer();
114             var url:String = connection.getParam("oauth_request_url", "http://127.0.0.1:3000/oauth/request_token");
115             
116             var params:Object = new Object();
117             var oauthRequest:OAuthRequest = new OAuthRequest("GET", url, params, consumer, null);
118             var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
119
120                         //register the "pressTry" function so the oAuth callback page can try to advance the editor directly to the next step
121                         ExternalInterface.addCallback("pressTry", pressTry);            
122                         
123             // build the actual request
124             var urlReq:URLRequest = new URLRequest(String(urlStr));
125             var loader:URLLoader = new URLLoader();
126             loader.addEventListener(Event.COMPLETE, loadedRequestToken);
127             loader.addEventListener(IOErrorEvent.IO_ERROR, requestTokenError);
128             loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, recordStatus);
129             loader.load(urlReq);
130         }
131         
132         private function recordStatus(event:HTTPStatusEvent):void {
133             lastHTTPStatus = event.status;
134         }
135         
136         private function requestTokenError(event:IOErrorEvent):void {
137             trace("error occured... last status was: "+lastHTTPStatus);
138             
139             if ( lastHTTPStatus == 401 ) {
140                 // this means authorisation was refused -- refused at this stage
141                 // means our consumer token is broken
142                 contentStack.selectedChild = permFailPanel;
143             } else {
144                 contentStack.selectedChild = tempFailPanel;
145             }
146             progress.visible = false;
147         }
148         
149         private function loadedRequestToken(event:Event):void {
150             trace("Yay! response: "+URLLoader(event.target).data);
151             requestToken = getResponseToken(URLLoader(event.target));
152             
153             var url:String = connection.getParam("oauth_auth_url", "http://127.0.0.1:3000/oauth/authorize");            
154             link.label = url;
155                         authoriseURL = url + "?oauth_token="+requestToken.key;
156                         progress.visible = false;
157             gotLinkBox.visible = true;
158         }
159
160         private function getResponseToken(loader:URLLoader):OAuthToken {
161             var vars:URLVariables = new URLVariables(loader.data);
162             
163             // build out request token
164             var token:OAuthToken = new OAuthToken(
165                 String(vars["oauth_token"]),
166                 String(vars["oauth_token_secret"]));
167             return token;
168         }
169         
170         private function getAccessToken():void {
171             var sig:IOAuthSignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
172             var consumer:OAuthConsumer = getConsumer();
173             var url:String = connection.getParam("oauth_access_url", "http://127.0.0.1:3000/oauth/access_token");
174
175             var oauthRequest:OAuthRequest = new OAuthRequest("GET", url, null, consumer, requestToken);
176             var urlStr:Object = oauthRequest.buildRequest(sig, OAuthRequest.RESULT_TYPE_URL_STRING)
177
178             var urlReq:URLRequest = new URLRequest(String(urlStr));
179             var loader:URLLoader = new URLLoader();
180             loader.addEventListener(Event.COMPLETE, loadedAccessToken);
181             loader.addEventListener(IOErrorEvent.IO_ERROR, accessTokenError);
182             loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, recordStatus);
183             loader.load(urlReq);
184             
185             progress.label = "Checking access";
186             progress.visible = true;  
187         }
188         
189         private function loadedAccessToken(event:Event):void {
190             trace("Yay! response: "+URLLoader(event.target).data);
191             progress.label = "Received Access";
192             progress.indeterminate = false;
193             progress.setProgress(100,100);
194             PopUpManager.removePopUp(this);
195             
196             _accessToken = getResponseToken(URLLoader(event.target));
197             connection.setAuthToken(_accessToken);
198             dispatchEvent(new Event(ACCESS_TOKEN_EVENT));
199         }
200         
201         public function get accessToken():OAuthToken {
202             return _accessToken;
203         }
204         
205         public function get shouldRemember():Boolean {
206             return rememberMe.selected;
207         }
208         
209         private function accessTokenError(event:IOErrorEvent):void {
210             if ( lastHTTPStatus == 401 ) {
211                 deniedLabel.htmlText = "<b>Access was denied, please check, and try again</b>";
212             } else {
213                 deniedLabel.htmlText = "<b>Error occurred</b> ("+lastHTTPStatus+"): please try again";
214             }
215             deniedLabel.visible = true;
216         }
217         
218         private function getConsumer():OAuthConsumer {
219             var key:String = connection.getParam("oauth_consumer_key", "");
220             var secret:String = connection.getParam("oauth_consumer_secret", "");
221             return new OAuthConsumer(key, secret);
222         }
223                 
224                 public function pressTry():void {
225                         getAccessToken();
226                 }
227         
228         ]]></mx:Script>
229 </mx:TitleWindow>
230