Skip to content

Commit

Permalink
[RFR-450] Add group paramter to registration (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
hb0 authored May 30, 2023
1 parent d3a428c commit ba0da1d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/de/cyface/uploader/Authenticator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ interface Authenticator {
* @param password The password part of the credentials
* @param captcha The captcha token
* @param activation The template to use for the activation email.
* @param group The database identifier of the group the user selected during registration
* @throws RegistrationFailed when an expected error occurred, so that the UI can handle this.
* @return [Result.UPLOAD_SUCCESSFUL] if successful.
*/
@Throws(RegistrationFailed::class)
fun register(email: String, password: String, captcha: String, activation: Activation): Result
fun register(email: String, password: String, captcha: String, activation: Activation, group: String): Result

/**
* @return the endpoint which will be used for authentication.
Expand Down
12 changes: 9 additions & 3 deletions src/main/kotlin/de/cyface/uploader/DefaultAuthenticator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import java.net.URL
* Usually the token should be generated just before each [DefaultUploader.upload] call.
*
* @author Armin Schnabel
* @version 1.0.0
* @version 1.0.1
* @since 1.0.0
* @property apiEndpoint An API endpoint running a Cyface data collector service, like `https://some.url/api/v3`
*/
Expand Down Expand Up @@ -117,13 +117,19 @@ class DefaultAuthenticator(private val apiEndpoint: String) : Authenticator {
}

@Suppress("CyclomaticComplexMethod")
override fun register(email: String, password: String, captcha: String, activation: Activation): Result {
override fun register(
email: String,
password: String,
captcha: String,
activation: Activation,
group: String
): Result {
var connection: HttpURLConnection? = null
try {
connection = http.open(registrationEndpoint(), false)

// Try to send the request and handle expected errors
val response = http.register(connection, email, password, captcha, activation)
val response = http.register(connection, email, password, captcha, activation, group)
LOGGER.debug("Response $response")
return response
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/de/cyface/uploader/Http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ interface Http {
* @param password The password part of the credentials
* @param captcha The captcha token
* @param activation The template to use for the activation email.
* @param group The database identifier of the group the user selected during registration
* @throws SynchronisationException If an IOException occurred while reading the response code.
* @throws BadRequestException When server returns `HttpURLConnection#HTTP_BAD_REQUEST`
* @throws UnauthorizedException When the server returns `HttpURLConnection#HTTP_UNAUTHORIZED`
Expand Down Expand Up @@ -137,6 +138,7 @@ interface Http {
email: String,
password: String,
captcha: String,
activation: Activation
activation: Activation,
group: String
): Result
}
15 changes: 11 additions & 4 deletions src/main/kotlin/de/cyface/uploader/HttpConnection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,13 @@ class HttpConnection : Http {
email: String,
password: String,
captcha: String,
activation: Activation
activation: Activation,
group: String
): Result {
// For performance reasons (documentation) set either fixedLength (known length) or chunked streaming mode
// we currently don't use fixedLengthStreamingMode as we only use this request for small login requests
connection.setChunkedStreamingMode(0)
val payload = registrationPayload(email, password, captcha, activation)
val payload = registrationPayload(email, password, captcha, activation, group)
val outputStream = initOutputStream(connection)
try {
outputStream.write(payload.toByteArray(DEFAULT_CHARSET))
Expand Down Expand Up @@ -172,9 +173,15 @@ class HttpConnection : Http {
return "{\"username\":\"$username\",\"password\":\"$password\"}"
}

private fun registrationPayload(email: String, password: String, captcha: String, template: Activation): String {
private fun registrationPayload(
email: String,
password: String,
captcha: String,
template: Activation,
group: String
): String {
return "{\"email\":\"$email\",\"password\":\"$password\",\"captcha\":\"$captcha\",\"template\":\"" +
"${template.name}\"}"
"${template.name}\",\"group\":\"$group\"}"
}

private fun gzip(input: ByteArray): ByteArray {
Expand Down

0 comments on commit ba0da1d

Please sign in to comment.