Skip to content

Commit

Permalink
Add portion of auth/application API. (#307)
Browse files Browse the repository at this point in the history
* Add portion of auth/application API.

Signed-off-by: Kanstantsin Shautsou <[email protected]>

* fixup
  • Loading branch information
KostyaSha authored and oleg-nenashev committed Nov 11, 2016
1 parent fa3d088 commit 24f48f6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/org/kohsuke/github/GHAuthorization.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ public class GHAuthorization extends GHObject {
private GitHub root;
private List<String> scopes;
private String token;
private String token_last_eight;
private String hashed_token;
private App app;
private String note;
private String note_url;
private String fingerprint;
//TODO add some user class for https://developer.github.com/v3/oauth_authorizations/#check-an-authorization ?
//private GHUser user;

public GitHub getRoot() {
return root;
Expand All @@ -52,6 +57,14 @@ public String getToken() {
return token;
}

public String getTokenLastEight() {
return token_last_eight;
}

public String getHashedToken() {
return hashed_token;
}

public URL getAppUrl() {
return GitHub.parseURL(app.url);
}
Expand Down Expand Up @@ -82,6 +95,10 @@ public URL getNoteUrl() {
return GitHub.parseURL(note_url);
}

public String getFingerprint() {
return fingerprint;
}

/*package*/ GHAuthorization wrap(GitHub root) {
this.root = root;
return this;
Expand All @@ -92,5 +109,6 @@ public URL getNoteUrl() {
private static class App {
private String url;
private String name;
private String client_id;
}
}
37 changes: 37 additions & 0 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import com.fasterxml.jackson.databind.introspect.VisibilityChecker.Std;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;

import javax.annotation.Nonnull;
import java.util.logging.Logger;

/**
Expand Down Expand Up @@ -497,6 +498,42 @@ public GHAuthorization createToken(Collection<String> scope, String note, String
return requester.method("POST").to("/authorizations", GHAuthorization.class).wrap(this);
}

/**
* @see <a href="https://developer.github.com/v3/oauth_authorizations/#get-or-create-an-authorization-for-a-specific-app">docs</a>
*/
public GHAuthorization createOrGetAuth(String clientId, String clientSecret, List<String> scopes, String note,
String note_url)
throws IOException {
Requester requester = new Requester(this)
.with("client_secret", clientSecret)
.with("scopes", scopes)
.with("note", note)
.with("note_url", note_url);

return requester.method("PUT").to("/authorizations/clients/" + clientId, GHAuthorization.class);
}

/**
* @see <a href="https://developer.github.com/v3/oauth_authorizations/#delete-an-authorization">Delete an authorization</a>
*/
public void deleteAuth(long id) throws IOException {
retrieve().method("DELETE").to("/authorizations/" + id);
}

/**
* @see <a href="https://developer.github.com/v3/oauth_authorizations/#check-an-authorization">Check an authorization</a>
*/
public GHAuthorization checkAuth(@Nonnull String clientId, @Nonnull String accessToken) throws IOException {
return retrieve().to("/applications/" + clientId + "/tokens/" + accessToken, GHAuthorization.class);
}

/**
* @see <a href="https://developer.github.com/v3/oauth_authorizations/#reset-an-authorization">Reset an authorization</a>
*/
public GHAuthorization resetAuth(@Nonnull String clientId, @Nonnull String accessToken) throws IOException {
return retrieve().method("POST").to("/applications/" + clientId + "/tokens/" + accessToken, GHAuthorization.class);
}

/**
* Ensures that the credential is valid.
*/
Expand Down

0 comments on commit 24f48f6

Please sign in to comment.