Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
hds
flexin
android-app
Commits
04b291a0
Commit
04b291a0
authored
Oct 25, 2017
by
Nastuzzi Samy
Browse files
Merge branch 'feature/connexionViaCAS' into develop
parents
d04a2180
6d4b5a0c
Changes
7
Hide whitespace changes
Inline
Side-by-side
app/src/main/java/fr/utc/simde/payutc/MainActivity.java
View file @
04b291a0
...
...
@@ -14,13 +14,17 @@ import android.widget.EditText;
import
android.widget.TextView
;
import
android.widget.Toast
;
import
java.io.IOException
;
import
fr.utc.simde.payutc.tools.HTTPRequest
;
import
fr.utc.simde.payutc.tools.NFCActivity
;
import
fr.utc.simde.payutc.tools.CASConnexion
;
import
fr.utc.simde.payutc.tools.Dialog
;
import
fr.utc.simde.payutc.tools.NemopaySession
;
public
class
MainActivity
extends
NFCActivity
{
private
static
final
String
LOG_TAG
=
"MainActivity"
;
private
static
final
String
LOG_TAG
=
"_MainActivity"
;
private
static
final
String
service
=
"http://assos.utc.fr"
;
private
static
Boolean
registered
=
false
;
private
static
Dialog
dialog
;
...
...
@@ -91,7 +95,7 @@ public class MainActivity extends NFCActivity {
if
(
casConnexion
.
isConnected
())
{
try
{
casConnexion
.
addService
();
casConnexion
.
addService
(
service
);
Thread
.
sleep
(
100
);
}
catch
(
Exception
e
)
{
Log
.
e
(
LOG_TAG
,
e
.
getMessage
());
...
...
@@ -100,15 +104,34 @@ public class MainActivity extends NFCActivity {
runOnUiThread
(
new
Runnable
()
{
@Override
public
void
run
()
{
loading
.
dismiss
();
if
(!
casConnexion
.
isServiceAdded
())
{
if
(
casConnexion
.
isServiceAdded
())
loading
.
setMessage
(
getResources
().
getString
(
R
.
string
.
nemopay_connection
));
else
{
loading
.
dismiss
();
dialog
.
errorDialog
(
getResources
().
getString
(
R
.
string
.
cas_connection
),
getResources
().
getString
(
R
.
string
.
cas_error_service_adding
));
}
else
Toast
.
makeText
(
MainActivity
.
this
,
"Connexion à réaliser avec Nemopay"
,
Toast
.
LENGTH_SHORT
).
show
();
// https://api.nemopay.net/services/POSS3/loginCas2?system_id=payutc
}
});
if
(
casConnexion
.
isServiceAdded
())
{
try
{
HTTPRequest
request
=
nemopaySession
.
loginCas
(
casConnexion
.
getTicket
(),
service
);
Thread
.
sleep
(
1000
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
runOnUiThread
(
new
Runnable
()
{
@Override
public
void
run
()
{
loading
.
dismiss
();
/*
if (casConnexion.isServiceAdded())
loading.setMessage(getResources().getString(R.string.nemopay_connection));*/
}
});
}
}
}
}.
start
();
...
...
app/src/main/java/fr/utc/simde/payutc/tools/CASConnexion.java
View file @
04b291a0
...
...
@@ -7,12 +7,11 @@ import android.util.Log;
*/
public
class
CASConnexion
{
private
static
final
String
LOG_TAG
=
"CASConnexion"
;
private
static
final
String
service
=
"http://api.nemopay.net/"
;
private
String
url
;
private
String
username
;
private
String
location
;
private
String
ticket
;
private
static
final
String
LOG_TAG
=
"_CASConnexion"
;
private
static
String
url
;
private
static
String
username
;
private
static
String
location
;
private
static
String
ticket
;
public
CASConnexion
(
final
NemopaySession
nemopaySession
)
{
this
.
url
=
""
;
...
...
@@ -24,8 +23,9 @@ public class CASConnexion {
@Override
public
void
run
()
{
try
{
HTTPRequest
request
=
nemopaySession
.
getCasUrl
();
url
=
request
.
getResponse
();
HTTPRequest
http
=
new
HTTPRequest
(
"https://api.nemopay.net/services/POSS3/getCasUrl?system_id=payutc"
);
// Remettre le getCasUrl
http
.
post
();
url
=
http
.
getResponse
();
url
=
url
.
substring
(
1
,
url
.
length
()
-
1
);
}
catch
(
Exception
e
)
{
Log
.
e
(
LOG_TAG
,
e
.
getMessage
());
...
...
@@ -36,6 +36,9 @@ public class CASConnexion {
public
void
setUsername
(
final
String
username
)
{
this
.
username
=
username
;
}
public
String
getUsername
()
{
return
this
.
username
;
}
public
String
getTicket
()
{
return
this
.
ticket
;
}
public
String
getUrl
()
{
return
this
.
url
;
}
public
void
connect
(
final
String
username
,
final
String
password
)
throws
Exception
{
...
...
@@ -47,8 +50,8 @@ public class CASConnexion {
throw
new
RuntimeException
(
"Elements required"
);
HTTPRequest
request
=
new
HTTPRequest
(
this
.
url
+
"v1/tickets/"
);
request
.
setArg
(
"username"
,
username
);
request
.
setArg
(
"password"
,
password
);
request
.
addPost
(
"username"
,
username
);
request
.
addPost
(
"password"
,
password
);
if
(
request
.
post
()
==
201
)
this
.
location
=
request
.
getHeader
(
"Location"
);
...
...
@@ -58,7 +61,7 @@ public class CASConnexion {
public
Boolean
isConnected
()
{
return
!
this
.
location
.
isEmpty
();
}
public
void
addService
()
throws
Exception
{
public
void
addService
(
final
String
service
)
throws
Exception
{
this
.
ticket
=
""
;
if
(!
isConnected
())
...
...
@@ -68,7 +71,7 @@ public class CASConnexion {
throw
new
RuntimeException
(
"Elements required"
);
HTTPRequest
request
=
new
HTTPRequest
(
this
.
location
);
request
.
setArg
(
"service"
,
this
.
service
);
request
.
addPost
(
"service"
,
service
);
if
(
request
.
post
()
==
200
)
this
.
ticket
=
request
.
getResponse
();
...
...
app/src/main/java/fr/utc/simde/payutc/tools/Dialog.java
View file @
04b291a0
...
...
@@ -18,7 +18,7 @@ import fr.utc.simde.payutc.R;
*/
public
class
Dialog
{
private
static
final
String
LOG_TAG
=
"Dialog"
;
private
static
final
String
LOG_TAG
=
"
_
Dialog"
;
private
static
Activity
activity
;
private
static
AlertDialog
alertDialog
;
private
static
AlertDialog
.
Builder
alertDialogBuilder
;
...
...
app/src/main/java/fr/utc/simde/payutc/tools/HTTPRequest.java
View file @
04b291a0
...
...
@@ -18,27 +18,29 @@ import java.util.List;
import
java.util.Map
;
public
class
HTTPRequest
{
p
ublic
static
final
String
LOG_TAG
=
"HTTPRequest"
;
p
ublic
String
url
;
p
ublic
HttpURLConnection
request
;
p
ublic
String
response
;
p
rivate
static
final
String
LOG_TAG
=
"
_
HTTPRequest"
;
p
rivate
String
url
;
p
rivate
HttpURLConnection
request
;
p
rivate
String
response
;
public
Map
<
String
,
String
>
args
;
public
static
Map
<
String
,
String
>
cookies
;
private
Map
<
String
,
String
>
postArgs
;
private
Map
<
String
,
String
>
getArgs
;
private
static
Map
<
String
,
String
>
cookies
;
public
HTTPRequest
(
final
String
url
)
{
this
.
url
=
url
;
this
.
request
=
null
;
this
.
response
=
""
;
this
.
args
=
new
HashMap
<
String
,
String
>();
this
.
postArgs
=
new
HashMap
<
String
,
String
>();
this
.
getArgs
=
new
HashMap
<
String
,
String
>();
this
.
cookies
=
new
HashMap
<
String
,
String
>();
}
public
int
get
()
throws
IOException
{
String
data
=
args2String
(
this
.
args
);
Log
.
d
(
LOG_TAG
,
"get: "
+
this
.
url
+
data
);
String
get
=
args2String
(
this
.
getArgs
,
true
);
Log
.
d
(
LOG_TAG
,
"get: "
+
this
.
url
+
get
);
this
.
request
=
(
HttpURLConnection
)
(
new
URL
(
this
.
url
+
"?"
+
data
)).
openConnection
();
this
.
request
=
(
HttpURLConnection
)
(
new
URL
(
this
.
url
+
get
)).
openConnection
();
this
.
request
.
setRequestMethod
(
"GET"
);
this
.
request
.
setRequestProperty
(
"Cookie"
,
getCookiesHeader
());
this
.
request
.
setUseCaches
(
false
);
...
...
@@ -46,25 +48,28 @@ public class HTTPRequest {
updateCookies
(
this
.
request
.
getHeaderFields
().
get
(
"Set-Cookie"
));
generateResponse
();
Log
.
d
(
LOG_TAG
,
"code: "
+
Integer
.
toString
(
this
.
request
.
getResponseCode
())
+
", response: "
+
this
.
getResponse
());
return
this
.
request
.
getResponseCode
();
}
public
int
post
()
throws
IOException
{
String
data
=
args2String
(
this
.
args
);
Log
.
d
(
LOG_TAG
,
"post: "
+
this
.
url
+
", data: "
+
data
);
String
get
=
args2String
(
this
.
getArgs
,
true
);
String
post
=
args2String
(
this
.
postArgs
);
Log
.
d
(
LOG_TAG
,
"post: "
+
this
.
url
+
get
+
", data: "
+
post
);
this
.
request
=
(
HttpURLConnection
)
(
new
URL
(
this
.
url
)).
openConnection
();
this
.
request
=
(
HttpURLConnection
)
(
new
URL
(
this
.
url
+
get
)).
openConnection
();
this
.
request
.
setRequestMethod
(
"POST"
);
this
.
request
.
setRequestProperty
(
"Content-Type"
,
"application/x-www-form-urlencoded"
);
this
.
request
.
setRequestProperty
(
"Content-Length"
,
Integer
.
toString
(
data
.
getBytes
().
length
));
this
.
request
.
setRequestProperty
(
"Content-Length"
,
Integer
.
toString
(
post
.
getBytes
().
length
));
this
.
request
.
setRequestProperty
(
"Cookie"
,
getCookiesHeader
());
this
.
request
.
setUseCaches
(
false
);
this
.
request
.
setDoInput
(
true
);
this
.
request
.
setDoOutput
(
true
);
this
.
request
.
getOutputStream
().
write
(
data
.
getBytes
());
this
.
request
.
getOutputStream
().
write
(
post
.
getBytes
());
updateCookies
(
this
.
request
.
getHeaderFields
().
get
(
"Set-Cookie"
));
generateResponse
();
Log
.
d
(
LOG_TAG
,
"code: "
+
Integer
.
toString
(
this
.
request
.
getResponseCode
())
+
", response: "
+
this
.
getResponse
());
return
this
.
request
.
getResponseCode
();
}
...
...
@@ -111,30 +116,30 @@ public class HTTPRequest {
public
String
getResponse
()
throws
IOException
{
return
response
;
}
protected
String
args2String
(
Map
<
String
,
String
>
args
)
throws
UnsupportedEncodingException
{
protected
String
args2String
(
Map
<
String
,
String
>
args
)
throws
UnsupportedEncodingException
{
return
args2String
(
args
,
false
);
}
protected
String
args2String
(
Map
<
String
,
String
>
args
,
Boolean
isGet
)
throws
UnsupportedEncodingException
{
String
data
=
""
;
for
(
String
arg
:
args
.
keySet
())
data
+=
(
URLEncoder
.
encode
(
arg
,
"UTF-8"
)
+
"="
+
URLEncoder
.
encode
(
args
.
get
(
arg
),
"UTF-8"
)
+
"&"
);
return
data
.
substring
(
0
,
data
.
equals
(
""
)
?
0
:
data
.
length
()
-
1
);
return
data
.
equals
(
""
)
?
""
:
(
isGet
?
"?"
:
""
)
+
data
.
substring
(
0
,
data
.
length
()
-
1
);
}
public
void
set
Args
(
Map
<
String
,
String
>
args
)
{
this
.
a
rgs
=
args
;
public
void
set
Get
(
Map
<
String
,
String
>
args
)
{
this
.
getA
rgs
=
args
;
}
public
void
set
Arg
(
final
String
key
,
final
String
value
)
{
this
.
args
.
put
(
key
,
value
)
;
public
void
set
Post
(
Map
<
String
,
String
>
args
)
{
this
.
postArgs
=
args
;
}
public
Boolean
delArg
(
final
String
key
)
{
if
(
this
.
args
.
containsKey
(
key
))
this
.
args
.
remove
(
key
);
else
return
false
;
public
void
addGet
(
final
String
key
,
final
String
value
)
{
this
.
getArgs
.
put
(
key
,
value
);
}
return
true
;
public
void
addPost
(
final
String
key
,
final
String
value
)
{
this
.
postArgs
.
put
(
key
,
value
);
}
synchronized
String
getCookiesHeader
()
{
...
...
app/src/main/java/fr/utc/simde/payutc/tools/NFCActivity.java
View file @
04b291a0
...
...
@@ -23,7 +23,7 @@ import android.widget.Toast;
import
fr.utc.simde.payutc.R
;
public
abstract
class
NFCActivity
extends
Activity
{
private
static
final
String
LOG_TAG
=
"NFCActivity"
;
private
static
final
String
LOG_TAG
=
"
_
NFCActivity"
;
private
static
NfcAdapter
NFCAdapter
;
private
static
AlertDialog
.
Builder
alertDialogBuilder
;
...
...
app/src/main/java/fr/utc/simde/payutc/tools/NemopaySession.java
View file @
04b291a0
...
...
@@ -12,13 +12,13 @@ import java.util.Map;
*/
public
class
NemopaySession
{
private
static
final
String
LOG_TAG
=
"NemopaySession"
;
private
static
final
String
LOG_TAG
=
"
_
NemopaySession"
;
private
static
final
String
url
=
"https://api.nemopay.net/services/"
;
private
String
key
;
private
String
session
;
private
String
username
;
private
final
Map
<
String
,
String
>
defaul
tArgs
=
new
HashMap
<
String
,
String
>()
{{
private
final
Map
<
String
,
String
>
ge
tArgs
=
new
HashMap
<
String
,
String
>()
{{
put
(
"system_id"
,
"payutc"
);
}};
...
...
@@ -38,14 +38,14 @@ public class NemopaySession {
}});
}
p
ublic
HTTPRequest
construct
(
final
String
method
,
final
String
service
)
throws
IOException
{
return
construct
(
method
,
service
,
new
HashMap
<
String
,
String
>());
}
p
ublic
HTTPRequest
construct
(
final
String
method
,
final
String
service
,
final
Map
<
String
,
String
>
a
rgs
)
throws
IOException
{
p
rotected
HTTPRequest
construct
(
final
String
method
,
final
String
service
)
throws
IOException
{
return
construct
(
method
,
service
,
new
HashMap
<
String
,
String
>());
}
p
rotected
HTTPRequest
construct
(
final
String
method
,
final
String
service
,
final
Map
<
String
,
String
>
postA
rgs
)
throws
IOException
{
HTTPRequest
request
=
new
HTTPRequest
(
url
+
method
+
"/"
+
service
);
request
.
setArgs
(
defaultArgs
);
for
(
String
arg
:
args
.
keySet
())
request
.
setArg
(
arg
,
args
.
get
(
arg
));
Log
.
d
(
LOG_TAG
,
"url: "
+
url
+
method
+
"/"
+
service
);
request
.
setGet
(
getArgs
);
request
.
setPost
(
postArgs
);
request
.
post
();
return
request
;
}
}
app/src/main/res/values/strings.xml
View file @
04b291a0
...
...
@@ -33,4 +33,5 @@
<string
name=
"cas_error_service_connection"
>
Erreur lors de la connexion au service depuis le CAS
</string>
<string
name=
"badge_connection"
>
Connexion via badge
</string>
<string
name=
"badge_app_not_registered"
>
Il est nécessaire que l\'application soit enregistrée pour permettre la connexion via badge
</string>
<string
name=
"nemopay_connection"
>
Connexion à Nemopay
</string>
</resources>
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment