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
+ document . getElementById ( "prediction_settings_message" ) . innerText = "Use this form to configure predictions for your launch. Please only use this for your own launches. Callsigns must match your payload callsigns exactly (case sensitive)."
31
+ }
32
+ } ) ;
33
+ function query_flight_doc ( ) {
34
+ var payload_callsign = document . getElementById ( "flight_doc_payload_callsign" ) . value
35
+ fetch ( "https://api.v2.sondehub.org/amateur/flightdoc/" + payload_callsign ) . then (
36
+ function ( response ) {
37
+ if ( response . ok ) {
38
+ response . text ( ) . then ( function ( x ) {
39
+ var data = JSON . parse ( x )
40
+ if ( data . float_expected ) {
41
+ document . getElementById ( "flight_doc_float_expected" ) . checked = true
42
+ } else {
43
+ document . getElementById ( "flight_doc_float_expected" ) . checked = false
44
+ }
45
+ document . getElementById ( "flight_doc_peak_altitude" ) . value = data . peak_altitude
46
+ document . getElementById ( "flight_doc_descent_rate" ) . value = data . descent_rate
47
+ document . getElementById ( "flight_doc_ascent_rate" ) . value = data . ascent_rate
48
+ } )
49
+ } else {
50
+ document . getElementById ( "payload-update-results" ) . textContent = "Could not load payload data"
51
+ }
52
+ }
53
+ )
54
+ }
55
+ function logout ( ) {
56
+ logout_url = "https://auth.v2.sondehub.org/logout?client_id=21dpr4kth8lonk2rq803loh5oa&response_type=token&logout_uri=" + window . location . protocol + "//" + window . location . host
57
+ sessionStorage . removeItem ( "id_token" )
58
+ window . location = logout_url
59
+ }
60
+
61
+ function update_flight_doc ( ) {
62
+
63
+ var body = JSON . stringify (
64
+ {
65
+ "payload_callsign" : document . getElementById ( "flight_doc_payload_callsign" ) . value ,
66
+ "float_expected" : document . getElementById ( "flight_doc_float_expected" ) . value == "on" ,
67
+ "peak_altitude" : parseFloat ( document . getElementById ( "flight_doc_peak_altitude" ) . value ) ,
68
+ "descent_rate" : parseFloat ( document . getElementById ( "flight_doc_descent_rate" ) . value ) ,
69
+ "ascent_rate" : parseFloat ( document . getElementById ( "flight_doc_ascent_rate" ) . value ) ,
70
+ }
71
+ )
72
+
73
+
74
+ var httpRequest = new AWS . HttpRequest ( "https://api-raw.v2.sondehub.org/amateur/flightdoc" , "us-east-1" ) ;
75
+ var v4signer = new AWS . Signers . V4 ( httpRequest , "execute-api" , true ) ;
76
+ httpRequest . method = "PUT" ;
77
+ httpRequest . headers [ 'Host' ] = 'api-raw.v2.sondehub.org' ;
78
+ httpRequest . headers [ 'Content-Type' ] = 'application/json' ;
79
+ httpRequest . headers [ 'Content-Length' ] = body . length ;
80
+ httpRequest . headers [ 'X-Amz-Content-Sha256' ] = v4signer . hexEncodedHash ( body )
81
+ httpRequest . body = body
82
+
83
+
84
+
85
+ v4signer . addAuthorization ( AWS . config . credentials , AWS . util . date . getDate ( ) ) ;
86
+ document . getElementById ( "payload-update-results" ) . textContent = "Updating..."
87
+ fetch ( httpRequest . endpoint . href , {
88
+ method : httpRequest . method ,
89
+ headers : httpRequest . headers ,
90
+ body : httpRequest . body ,
91
+ } ) . then ( function ( response ) {
92
+ if ( ! response . ok ) {
93
+ response . text ( ) . then ( function ( x ) { document . getElementById ( "payload-update-results" ) . textContent = x } )
94
+ return ;
95
+ }
96
+ response . text ( ) . then ( function ( x ) { document . getElementById ( "payload-update-results" ) . textContent = x } )
97
+ } ) ;
98
+
99
+ }
0 commit comments