Skip to content

Commit

Permalink
Remove apiEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Dec 22, 2023
1 parent 0d1c525 commit 77d23a3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
41 changes: 31 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
# casdoor-android-sdk
Casdoor's SDK for Android will allow you to easily connect your application to the Casdoor authentication system without having to implement it from scratch.

Casdoor's SDK for Android will allow you to easily connect your application to the Casdoor
authentication system without having to implement it from scratch.
Casdoor SDK is simple to use. We will show you the steps below.

## Step0. Adding the dependency

Add the following dependency to your app's build.gradle file:

```groovy
dependencies {
implementation 'org.casdoor:casdoor-android-sdk:x.x.x' // not upload to maven yet...
}
```

## Step1. Init Config

Initialization requires 5 parameters, which are all str type:
| Name (in order) | Must | Description |
| Name (in order) | Must | Description |
| ---------------- | ---- | --------------------------------------------------- |
| endpoint | Yes | Casdoor Server Url, such as `http://localhost:8000` |
| clientID | Yes | Application.clientID |
| appName | Yes | Application.name |
| apiEndpoint | NO | Casdoor Api Url, default endpoint + "/api/" |
| organizationName | Yes |Organization name
| endpoint | Yes | Casdoor Server Url, such as `http://localhost:8000` |
| clientID | Yes | Application.clientID |
| appName | Yes | Application.name |
| organizationName | Yes |Organization name

```kotlin
val casdoorConfig = CasdoorConfig(
endpoint = "http://localhost:8000",
Expand All @@ -26,12 +33,17 @@ val casdoorConfig = CasdoorConfig(
appName = "application_y38644"
)
```

## Step2. Init Casdoor

The Casdoor Contains all APIs

```kotlin
val casdoor = Casdoor(casdoorConfig)
```

## Step3. Authorize with the Casdoor server

At this point, we should use some ways to verify with the Casdoor server.

To start, we want you understand clearly the verification process of Casdoor.
Expand All @@ -42,15 +54,24 @@ provider, you cannot use request management service like Postman to send a URL
with parameters and get back a JSON file.

casdoor-android-sdk support the url,you can use in webview or browser to verify.

```kotlin
casdoor.getSignInUrl()
```

Hints:

1. `redirect_uri` is the URL that your `APP` is configured to
listen to the response from `Casdoor`. For example, if your `redirect_uri` is `casdoor://callback`, then Casdoor will send a request to this URL along with two parameters `code` and `state`, which will be used in later steps for authentication.
2. `state` is usually your Application's name, you can find it under the `Applications` tab in `Casdoor`, and the leftmost `Name` column gives each application's name.
listen to the response from `Casdoor`. For example, if your `redirect_uri`
is `casdoor://callback`, then Casdoor will send a request to this URL along with two
parameters `code` and `state`, which will be used in later steps for authentication.
2. `state` is usually your Application's name, you can find it under the `Applications` tab
in `Casdoor`, and the leftmost `Name` column gives each application's name.
3. The authorize URL allows the user to connect to a provider and give access to your application.
4. After Casdoor verification passed, it will be redirected to your `redirect_uri`, like `casdoor://callback?code=xxx&state=yyyy`.you can catch it and get the `code` and `state`, then call `requestOauthAccessToken()` and parse out jwt token.
4. After Casdoor verification passed, it will be redirected to your `redirect_uri`,
like `casdoor://callback?code=xxx&state=yyyy`.you can catch it and get the `code` and `state`,
then call `requestOauthAccessToken()` and parse out jwt token.

# Example

See at: https://github.com/casdoor/casdoor-android-example
6 changes: 3 additions & 3 deletions library/src/main/java/org/casdoor/Casdoor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Casdoor(private val config: CasdoorConfig) {
*/
fun requestOauthAccessToken(code: String): AccessTokenResponse {

var httpUrl = "${config.apiEndpoint}login/oauth/access_token".toHttpUrlOrNull()
var httpUrl = "${config.endpoint}login/oauth/access_token".toHttpUrlOrNull()
httpUrl ?: throw IllegalArgumentException("Invalid URL")
httpUrl = AccessTokenRequest(
code = code,
Expand Down Expand Up @@ -110,7 +110,7 @@ class Casdoor(private val config: CasdoorConfig) {
*/
fun renewToken(refreshToken: String, scope: String? = null): AccessTokenResponse {

var httpUrl = "${config.apiEndpoint}login/oauth/refresh_token".toHttpUrlOrNull()
var httpUrl = "${config.endpoint}login/oauth/refresh_token".toHttpUrlOrNull()
httpUrl ?: throw IllegalArgumentException("Invalid URL")
httpUrl = RenewAccessTokenRequest(
refreshToken = refreshToken,
Expand Down Expand Up @@ -138,7 +138,7 @@ class Casdoor(private val config: CasdoorConfig) {
* Logout.
*/
fun logout(idToken: String, state: String? = null): Boolean {
var httpUrl = "${config.apiEndpoint}login/oauth/logout".toHttpUrlOrNull()
var httpUrl = "${config.endpoint}login/oauth/logout".toHttpUrlOrNull()
httpUrl ?: throw IllegalArgumentException("Invalid URL")
httpUrl = httpUrl.newBuilder()
.addQueryParameter("id_token_hint", idToken)
Expand Down
6 changes: 0 additions & 6 deletions library/src/main/java/org/casdoor/CasdoorConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@ data class CasdoorConfig(
val redirectUri: String,
var endpoint: String,
val appName: String,
var apiEndpoint: String? = null
) {


init {
endpoint = formatEndpoint(endpoint)
apiEndpoint = if (apiEndpoint != null) {
formatEndpoint(apiEndpoint!!)
} else {
"${endpoint}api/"
}
}

private fun formatEndpoint(url: String): String {
Expand Down
15 changes: 2 additions & 13 deletions library/src/test/java/org/casdoor/CasdoorConfigTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,10 @@ class CasdoorConfigTest {
clientID = "",
organizationName = "",
redirectUri = "",
endpoint = "http://bar.com/",
endpoint = "http://bar.com",
appName = ""
)

assertEquals("http://bar.com/api/", casdoorConfig1.apiEndpoint)

val casdoorConfig2 = CasdoorConfig(
clientID = "",
organizationName = "",
redirectUri = "",
endpoint = "",
appName = "",
apiEndpoint = "http://foo.com"
)
assertEquals("http://foo.com/", casdoorConfig2.apiEndpoint)

assertEquals("http://bar.com", casdoorConfig1.endpoint)
}
}

0 comments on commit 77d23a3

Please sign in to comment.