Skip to content

Commit eb44170

Browse files
committed
feat: create OAuthRequestFactory#getLoginUrl
1 parent 12f227b commit eb44170

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

src/main/java/io/github/yvasyliev/deezer/exception/DeezerException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ public DeezerException(String message) {
88
public DeezerException(Throwable cause) {
99
super(cause);
1010
}
11+
12+
public DeezerException(String message, Throwable cause) {
13+
super(message, cause);
14+
}
1115
}

src/main/java/io/github/yvasyliev/deezer/factory/OAuthRequestFactory.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,64 @@
11
package io.github.yvasyliev.deezer.factory;
22

3+
import io.github.yvasyliev.deezer.exception.DeezerException;
34
import io.github.yvasyliev.deezer.model.AccessToken;
5+
import io.github.yvasyliev.deezer.model.Permission;
46
import io.github.yvasyliev.deezer.request.DeezerRequest;
57
import io.github.yvasyliev.deezer.request.SimpleDeezerRequest;
68
import io.github.yvasyliev.deezer.service.OAuthService;
79
import lombok.RequiredArgsConstructor;
810

11+
import java.net.MalformedURLException;
12+
import java.net.URI;
13+
import java.net.URL;
14+
import java.net.URLEncoder;
15+
import java.nio.charset.StandardCharsets;
16+
import java.util.Collection;
17+
import java.util.stream.Collectors;
18+
19+
/**
20+
* Factory for creating OAuth-related requests.
21+
*/
922
@RequiredArgsConstructor
1023
public class OAuthRequestFactory {
24+
private static final String LOGIN_URL_TEMPLATE = "%s/oauth/auth.php?app_id=%d&redirect_uri=%s&perms=%s";
25+
private final URL authHost;
1126
private final OAuthService oAuthService;
1227

28+
/**
29+
* Generates the login URL for the OAuth flow.
30+
*
31+
* @param appId the application ID
32+
* @param redirectUri the URI to redirect to after login
33+
* @param permissions the permissions to request
34+
* @return the login URL
35+
* @throws DeezerException if URL generation fails
36+
*/
37+
public URL getLoginUrl(int appId, String redirectUri, Collection<Permission> permissions)
38+
throws DeezerException {
39+
try {
40+
var perms = permissions.stream().map(Permission::getValue).collect(Collectors.joining(","));
41+
var loginUrl = LOGIN_URL_TEMPLATE.formatted(
42+
authHost,
43+
appId,
44+
URLEncoder.encode(redirectUri, StandardCharsets.UTF_8),
45+
URLEncoder.encode(perms, StandardCharsets.UTF_8)
46+
);
47+
48+
return URI.create(loginUrl).toURL();
49+
} catch (MalformedURLException e) {
50+
throw new DeezerException("Failed to generate login URL", e);
51+
}
52+
}
53+
54+
/**
55+
* Exchanges the authorization code for an access token.
56+
*
57+
* @param appId the application ID
58+
* @param secret the application secret
59+
* @param code the authorization code received from the login flow
60+
* @return an access token request
61+
*/
1362
public DeezerRequest<AccessToken> getAccessToken(int appId, String secret, String code) {
1463
return new SimpleDeezerRequest<>(() -> oAuthService.getAccessTokenAsync(appId, secret, code));
1564
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.github.yvasyliev.deezer.model;
2+
3+
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
5+
6+
/**
7+
* Permissions that can be requested by the application.
8+
*/
9+
@RequiredArgsConstructor
10+
@Getter
11+
public enum Permission {
12+
/**
13+
* Access users basic information.
14+
*/
15+
BASIC_ACCESS("basic_access"),
16+
17+
/**
18+
* Get the user's email.
19+
*/
20+
EMAIL("email"),
21+
22+
/**
23+
* Access user data any time.
24+
*/
25+
OFFLINE_ACCESS("offline_access"),
26+
27+
/**
28+
* Manage users' library.
29+
*/
30+
MANAGE_LIBRARY("manage_library"),
31+
32+
/**
33+
* Manage users' friends.
34+
*/
35+
MANAGE_COMMUNITY("manage_community"),
36+
37+
/**
38+
* Delete library items.
39+
*/
40+
DELETE_LIBRARY("delete_library"),
41+
42+
/**
43+
* Allow the application to access the user's listening history.
44+
*/
45+
LISTENING_HISTORY("listening_history");
46+
47+
private final String value;
48+
}

0 commit comments

Comments
 (0)