Skip to content

Commit

Permalink
add email field to VKOAuth2AccessToken (thanks to https://github.com/…
Browse files Browse the repository at this point in the history
  • Loading branch information
kullfar committed Mar 22, 2018
1 parent 787668d commit 71f8539
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 3 deletions.
3 changes: 2 additions & 1 deletion changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[SNAPSHOT]
* fix missing support for scope for refresh_token grant_type [thanks to https://github.com/tlxtellef]
* fix missing support for scope for refresh_token grant_type (thanks to https://github.com/tlxtellef)
* add email field to VKOAuth2AccessToken (thanks to https://github.com/grouzen)

[5.3.0]
* fix Salesforce API (thanks to https://github.com/jhorowitz-firedrum)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.github.scribejava.apis;

import com.github.scribejava.apis.vk.VKJsonTokenExtractor;
import com.github.scribejava.core.builder.api.ClientAuthenticationType;
import com.github.scribejava.core.builder.api.DefaultApi20;
import com.github.scribejava.core.builder.api.OAuth2SignatureType;
import com.github.scribejava.core.extractors.TokenExtractor;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.model.Verb;

public class VkontakteApi extends DefaultApi20 {
Expand All @@ -13,6 +16,7 @@ protected VkontakteApi() {
}

private static class InstanceHolder {

private static final VkontakteApi INSTANCE = new VkontakteApi();
}

Expand Down Expand Up @@ -44,4 +48,9 @@ public OAuth2SignatureType getSignatureType() {
public ClientAuthenticationType getClientAuthenticationType() {
return ClientAuthenticationType.REQUEST_BODY;
}

@Override
public TokenExtractor<OAuth2AccessToken> getAccessTokenExtractor() {
return VKJsonTokenExtractor.instance();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.scribejava.apis.vk;

import com.github.scribejava.core.extractors.OAuth2AccessTokenJsonExtractor;
import java.util.regex.Pattern;

/**
* additionally parses email
*/
public class VKJsonTokenExtractor extends OAuth2AccessTokenJsonExtractor {

private static final Pattern EMAIL_REGEX_PATTERN = Pattern.compile("\"email\"\\s*:\\s*\"(\\S*?)\"");

protected VKJsonTokenExtractor() {
}

private static class InstanceHolder {

private static final VKJsonTokenExtractor INSTANCE = new VKJsonTokenExtractor();
}

public static VKJsonTokenExtractor instance() {
return InstanceHolder.INSTANCE;
}

@Override
protected VKOAuth2AccessToken createToken(String accessToken, String tokenType, Integer expiresIn,
String refreshToken, String scope, String response) {
return new VKOAuth2AccessToken(accessToken, tokenType, expiresIn, refreshToken, scope,
extractParameter(response, EMAIL_REGEX_PATTERN, false), response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.scribejava.apis.vk;

import com.github.scribejava.core.model.OAuth2AccessToken;
import java.util.Objects;

public class VKOAuth2AccessToken extends OAuth2AccessToken {

private static final long serialVersionUID = -3539517142527580499L;

private final String email;

public VKOAuth2AccessToken(String accessToken, String email, String rawResponse) {
this(accessToken, null, null, null, null, email, rawResponse);
}

public VKOAuth2AccessToken(String accessToken, String tokenType, Integer expiresIn, String refreshToken,
String scope, String email, String rawResponse) {
super(accessToken, tokenType, expiresIn, refreshToken, scope, rawResponse);
this.email = email;
}

public String getEmail() {
return email;
}

@Override
public int hashCode() {
int hash = super.hashCode();
hash = 37 * hash + Objects.hashCode(email);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
if (!super.equals(obj)) {
return false;
}

return Objects.equals(email, ((VKOAuth2AccessToken) obj).getEmail());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Scanner;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.apis.VkontakteApi;
import com.github.scribejava.apis.vk.VKOAuth2AccessToken;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
Expand All @@ -26,7 +27,7 @@ public static void main(String... args) throws IOException, InterruptedException
final String clientSecret = "your client secret";
final OAuth20Service service = new ServiceBuilder(clientId)
.apiSecret(clientSecret)
.scope("wall,offline") // replace with desired scope
.scope("wall,offline,email") // replace with desired scope
.callback("http://your.site.com/callback")
.build(VkontakteApi.instance());
final Scanner in = new Scanner(System.in);
Expand All @@ -50,6 +51,10 @@ public static void main(String... args) throws IOException, InterruptedException
final OAuth2AccessToken accessToken = service.getAccessToken(code);
System.out.println("Got the Access Token!");
System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')");
if (accessToken instanceof VKOAuth2AccessToken) {
System.out.println("it's a VKOAuth2AccessToken, it has email field = '"
+ ((VKOAuth2AccessToken) accessToken).getEmail() + "'.");
}
System.out.println();

// Now let's go and ask for a protected resource!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public OAuth2AccessToken(String accessToken, String tokenType, Integer expiresIn
this.scope = scope;
}


public String getAccessToken() {
return accessToken;
}
Expand Down

0 comments on commit 71f8539

Please sign in to comment.