1+
2+ // populate login url
3+ document . getElementById ( "login_url" ) . href = "https://auth.v2.sondehub.org/oauth2/authorize?client_id=21dpr4kth8lonk2rq803loh5oa&response_type=token&scope=email+openid+phone&redirect_uri=" + window . location . protocol + "//" + window . location . host
4+
5+ // manage AWS cognito auth
6+ if ( window . location . hash . indexOf ( "id_token" ) != - 1 ) {
7+ console . log ( "Detected login" )
8+ var args = window . location . hash . slice ( 1 )
9+ var parms = new URLSearchParams ( args )
10+ var id_token = parms . get ( "id_token" )
11+ sessionStorage . setItem ( "id_token" , id_token )
12+ }
13+
14+ // do AWS login
15+ AWS . config . region = 'us-east-1' ;
16+
17+ AWS . config . credentials = new AWS . CognitoIdentityCredentials ( {
18+ IdentityPoolId : 'us-east-1:55e43eac-9626-43e1-a7d2-bbc57f5f5aa9' ,
19+ Logins : {
20+ "cognito-idp.us-east-1.amazonaws.com/us-east-1_G4H7NMniM" : sessionStorage . getItem ( "id_token" )
21+ }
22+ } ) ;
23+
24+ AWS . config . credentials . get ( function ( ) {
25+ // if this passes we update the login page to say logged in
26+ if ( AWS . config . credentials . accessKeyId != undefined ) {
27+ document . getElementById ( "login_url" ) . innerText = "Logout"
28+ document . getElementById ( "login_url" ) . href = "javascript:logout()"
29+ document . getElementById ( "update-flightdocs" ) . style . display = "block"
30+ }
31+ } ) ;
32+
33+ function logout ( ) {
34+ logout_url = "https://auth.v2.sondehub.org/logout?client_id=21dpr4kth8lonk2rq803loh5oa&response_type=token&logout_uri=" + window . location . protocol + "//" + window . location . host
35+ sessionStorage . removeItem ( "id_token" )
36+ window . location = logout_url
37+ }
38+
39+ function update_flight_doc ( ) {
40+
41+ var body = JSON . stringify (
42+ {
43+ "payload_callsign" : document . getElementById ( "flight_doc_payload_callsign" ) . value ,
44+ "float_expected" : document . getElementById ( "flight_doc_float_expected" ) . value == "on" ,
45+ "peak_altitude" : parseFloat ( document . getElementById ( "flight_doc_peak_altitude" ) . value ) ,
46+ "descent_rate" : parseFloat ( document . getElementById ( "flight_doc_descent_rate" ) . value ) ,
47+ "ascent_rate" : parseFloat ( document . getElementById ( "flight_doc_ascent_rate" ) . value ) ,
48+ }
49+ )
50+
51+
52+ var httpRequest = new AWS . HttpRequest ( "https://api-raw.v2.sondehub.org/amateur/flightdoc" , "us-east-1" ) ;
53+ var v4signer = new AWS . Signers . V4 ( httpRequest , "execute-api" , true ) ;
54+ httpRequest . method = "PUT" ;
55+ httpRequest . headers [ 'Host' ] = 'api-raw.v2.sondehub.org' ;
56+ httpRequest . headers [ 'Content-Type' ] = 'application/json' ;
57+ httpRequest . headers [ 'Content-Length' ] = body . length ;
58+ httpRequest . headers [ 'X-Amz-Content-Sha256' ] = v4signer . hexEncodedHash ( body )
59+ httpRequest . body = body
60+
61+
62+
63+ v4signer . addAuthorization ( AWS . config . credentials , AWS . util . date . getDate ( ) ) ;
64+ document . getElementById ( "payload-update-results" ) . textContent = "Updating..."
65+ fetch ( httpRequest . endpoint . href , {
66+ method : httpRequest . method ,
67+ headers : httpRequest . headers ,
68+ body : httpRequest . body ,
69+ } ) . then ( function ( response ) {
70+ if ( ! response . ok ) {
71+ response . text ( ) . then ( function ( x ) { document . getElementById ( "payload-update-results" ) . textContent = x } )
72+ return ;
73+ }
74+ response . text ( ) . then ( function ( x ) { document . getElementById ( "payload-update-results" ) . textContent = x } )
75+ } ) ;
76+
77+ }
0 commit comments