From 4121cae121f325b53e1b02934ccf6ba761997ef5 Mon Sep 17 00:00:00 2001 From: Derek Lawless Date: Mon, 4 Jan 2016 23:20:44 -0600 Subject: [PATCH 1/6] Initial creation of new gapi.auth2 types --- gapi.auth2/gapi.auth2.d.ts | 75 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 gapi.auth2/gapi.auth2.d.ts diff --git a/gapi.auth2/gapi.auth2.d.ts b/gapi.auth2/gapi.auth2.d.ts new file mode 100644 index 000000000..7da32c9a1 --- /dev/null +++ b/gapi.auth2/gapi.auth2.d.ts @@ -0,0 +1,75 @@ +// Type definitions for Google Sign-In API +// Project: https://developers.google.com/identity/sign-in/web/ +// Definitions by: Derek Lawless +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module gapi.auth2 { + + // TODO + export class GoogleAuth { + + } + + // TODO + export class GoogleUser { + + } + + export function init(params: { + /** + * The app's client ID, found and created in the Google Developers Console. + */ + client_id?: string; + + /** + * The domains for which to create sign-in cookies. Either a URI, single_host_origin, or none. + * Defaults to single_host_origin if unspecified. + */ + cookie_policy?: string; + + /** + * The scopes to request, as a space-delimited string. Optional if fetch_basic_profile is not set to false. + */ + scope?: string; + + /** + * Fetch users' basic profile information when they sign in. Adds 'profile' and 'email' to the requested scopes. True if unspecified. + */ + fetch_basic_profile?: boolean; + + /** + * The Google Apps domain to which users must belong to sign in. This is susceptible to modification by clients, + * so be sure to verify the hosted domain property of the returned user. Use GoogleUser.getHostedDomain() on the client, + * and the hd claim in the ID Token on the server to verify the domain is what you expected. + */ + hosted_domain?: string; + + /** + * Used only for OpenID 2.0 client migration. Set to the value of the realm that you are currently using for OpenID 2.0, + * as described in OpenID 2.0 (Migration) https://developers.google.com/accounts/docs/OpenID#openid-connect. + */ + openid_realm?: string; + }): GoogleAuth; + + // TODO + export function getAuthInstance(); +} + +declare module gapi.signin { + // TODO + export function render(id: any, options: { + /** + * The auth scope or scopes to authorize. Auth scopes for individual APIs can be found in their documentation. + */ + scope?: string; + width?: number; + height?: number; + longtitle?: boolean; + theme?: string; + onsuccess?: any; + onfailure?: any; + app_package_name?: string; + }); +} From 15e8b662535b2c6b84dc9d5ff4d3eb9371683630 Mon Sep 17 00:00:00 2001 From: Derek Lawless Date: Sat, 16 Jan 2016 13:07:16 -0600 Subject: [PATCH 2/6] Add auth2 typings file --- gapi.auth2/gapi.auth2.d.ts | 223 +++++++++++++++++++++++++++++++++++-- 1 file changed, 215 insertions(+), 8 deletions(-) diff --git a/gapi.auth2/gapi.auth2.d.ts b/gapi.auth2/gapi.auth2.d.ts index 7da32c9a1..c5375f9fe 100644 --- a/gapi.auth2/gapi.auth2.d.ts +++ b/gapi.auth2/gapi.auth2.d.ts @@ -7,16 +7,190 @@ declare module gapi.auth2 { - // TODO + /** + * GoogleAuth is a singleton class that provides methods to allow the user to sign in with a Google account, + * get the user's current sign-in status, get specific data from the user's Google profile, + * request additional scopes, and sign out from the current account. + */ export class GoogleAuth { + /** + * Calls the onInit function when the GoogleAuth object is fully initialized, or calls the onFailure function if + * initialization fails. + */ + then(onInit: () => any, onFailure: (reason: string) => any); + /** + * Signs in the user with the options specified to gapi.auth2.init(). + */ + signIn(): any; + + /** + * Signs in the user using the specified options. + */ + signIn(options?: { + app_package_name?: string; + fetch_basic_profile?: boolean; + prompt?: boolean; + scope?: string; + }, optionBuilder?: SigninOptionsBuilder): any; + + /** + * Signs out all accounts from the application. + */ + signOut(): any; + + /** + * Revokes all of the scopes that the user granted. + */ + disconnect(): any; + + /** + * Get permission from the user to access the specified scopes offline. + */ + grantOfflineAccess(options: { + scope?: string; + redirect_uri?: string; + }): any; + + /** + * Attaches the sign-in flow to the specified container's click handler. + */ + attachClickHandler(container: any, options: { + app_package_name?: string; + fetch_basic_profile?: boolean; + prompt?: boolean; + scope?: string; + }, onsuccess: () => any, onfailure: (reason: string) => any): any; } - // TODO - export class GoogleUser { + export module GoogleAuth { + export interface isSignedIn{ + /** + * Returns whether the current user is currently signed in. + */ + get(): boolean; + /** + * Listen for changes in the current user's sign-in state. + */ + listen(listener: (signedIn: boolean) => any): void; + } + + export interface currentUser { + /** + * Returns a GoogleUser object that represents the current user. Note that in a newly-initialized + * GoogleAuth instance, the current user has not been set. Use the currentUser.listen() method or the + * GoogleAuth.then() to get an initialized GoogleAuth instance. + */ + get(): GoogleUser; + + /** + * Listen for changes in currentUser. + */ + listen(listener: (user: GoogleUser) => any): void; + } } + export class SigninOptionsBuilder { + setAppPackageName(name: string): any; + setFetchBasicProfile(fetch: boolean): any; + setPrompt(prompt: string): any; + setScope(scope: string): any; + } + + export interface BasicProfile { + getId(): string; + getName(): string; + getImageUrl(): string; + getEmail(): string; + } + + export interface AuthResponse { + access_token: string; + id_token: string; + login_hint: string; + scope: string; + expires_in: string; + first_issued_at: string; + expires_at: string; + } + + /** + * A GoogleUser object represents one user account. + */ + export interface GoogleUser { + /** + * Get the user's unique ID string. + */ + getId(): string; + + /** + * Returns true if the user is signed in. + */ + isSignedIn(): boolean; + + /** + * Get the user's Google Apps domain if the user signed in with a Google Apps account. + */ + getHostedDomain(): string; + + /** + * Get the scopes that the user granted as a space-delimited string. + */ + getGrantedScopes(): string; + + /** + * Get the user's basic profile information. + */ + getBasicProfile(): BasicProfile; + + /** + * Get the response object from the user's auth session. + */ + getAuthResponse(): AuthResponse; + + /** + * Returns true if the user granted the specified scopes. + */ + hasGrantedScopes(scopes: string): boolean; + + /** + * Signs in the user. Use this method to request additional scopes for incremental + * authorization or to sign in a user after the user has signed out. + * When you use GoogleUser.signIn(), the sign-in flow skips the account chooser step. + * See GoogleAuth.signIn(). + */ + signIn(options?: { + app_package_name?: string; + fetch_basic_profile?: boolean; + prompt?: boolean; + scope?: string; + }, optionBuilder?: SigninOptionsBuilder): any; + + /** + * + */ + grant(options?: { + app_package_name?: string; + fetch_basic_profile?: boolean; + prompt?: boolean; + scope?: string; + }, optionBuilder?: SigninOptionsBuilder): any; + + /** + * Get permission from the user to access the specified scopes offline. + * When you use GoogleUser.grantOfflineAccess(), the sign-in flow skips the account chooser step. + * See GoogleUser.grantOfflineAccess(). + */ + grantOfflineAccess(scopes: string): void; + + /** + * Revokes all of the scopes that the user granted. + */ + disconnect(): void; + } + + export function init(params: { /** * The app's client ID, found and created in the Google Developers Console. @@ -48,28 +222,61 @@ declare module gapi.auth2 { /** * Used only for OpenID 2.0 client migration. Set to the value of the realm that you are currently using for OpenID 2.0, - * as described in OpenID 2.0 (Migration) https://developers.google.com/accounts/docs/OpenID#openid-connect. + * as described in OpenID 2.0 (Migration). */ openid_realm?: string; }): GoogleAuth; - // TODO - export function getAuthInstance(); + /** + * Returns the GoogleAuth object. You must initialize the GoogleAuth object with gapi.auth2.init() before calling this method. + */ + export function getAuthInstance(): GoogleAuth; } -declare module gapi.signin { - // TODO +declare module gapi.signin2 { + export function render(id: any, options: { /** * The auth scope or scopes to authorize. Auth scopes for individual APIs can be found in their documentation. */ scope?: string; + + /** + * The width of the button in pixels (default: 120). + */ width?: number; + + /** + * The height of the button in pixels (default: 36). + */ height?: number; + + /** + * Display long labels such as "Sign in with Google" rather than "Sign in" (default: false). + */ longtitle?: boolean; + + /** + * The color theme of the button: either light or dark (default: light). + */ theme?: string; + + /** + * The callback function to call when a user successfully signs in. + * This function must take one argument: an instance of gapi.auth2.GoogleUser (default: none). + */ onsuccess?: any; + + /** + * The callback function to call when sign-in fails. This function takes no arguments (default: none). + */ onfailure?: any; + + /** + * The package name of the Android app to install over the air. See + * Android app installs from your web site. + * Optional. (default: none) + */ app_package_name?: string; }); } From 64e0ede379b789efe9dc22a1d3c69892d3f639ed Mon Sep 17 00:00:00 2001 From: Derek Lawless Date: Sat, 23 Jan 2016 09:59:00 -0600 Subject: [PATCH 3/6] Let's try this as an interface instead --- gapi.auth2/gapi.auth2.d.ts | 48 ++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/gapi.auth2/gapi.auth2.d.ts b/gapi.auth2/gapi.auth2.d.ts index c5375f9fe..a133d22e4 100644 --- a/gapi.auth2/gapi.auth2.d.ts +++ b/gapi.auth2/gapi.auth2.d.ts @@ -13,6 +13,10 @@ declare module gapi.auth2 { * request additional scopes, and sign out from the current account. */ export class GoogleAuth { + isSignedIn: IsSignedIn; + + curretUser: CurrentUser; + /** * Calls the onInit function when the GoogleAuth object is fully initialized, or calls the onFailure function if * initialization fails. @@ -63,32 +67,30 @@ declare module gapi.auth2 { }, onsuccess: () => any, onfailure: (reason: string) => any): any; } - export module GoogleAuth { - export interface isSignedIn{ - /** - * Returns whether the current user is currently signed in. - */ - get(): boolean; + export interface IsSignedIn{ + /** + * Returns whether the current user is currently signed in. + */ + get(): boolean; - /** - * Listen for changes in the current user's sign-in state. - */ - listen(listener: (signedIn: boolean) => any): void; - } + /** + * Listen for changes in the current user's sign-in state. + */ + listen(listener: (signedIn: boolean) => any): void; + } - export interface currentUser { - /** - * Returns a GoogleUser object that represents the current user. Note that in a newly-initialized - * GoogleAuth instance, the current user has not been set. Use the currentUser.listen() method or the - * GoogleAuth.then() to get an initialized GoogleAuth instance. - */ - get(): GoogleUser; + export interface CurrentUser { + /** + * Returns a GoogleUser object that represents the current user. Note that in a newly-initialized + * GoogleAuth instance, the current user has not been set. Use the currentUser.listen() method or the + * GoogleAuth.then() to get an initialized GoogleAuth instance. + */ + get(): GoogleUser; - /** - * Listen for changes in currentUser. - */ - listen(listener: (user: GoogleUser) => any): void; - } + /** + * Listen for changes in currentUser. + */ + listen(listener: (user: GoogleUser) => any): void; } export class SigninOptionsBuilder { From 07044c9da023ce58e46ea596711110a324d98166 Mon Sep 17 00:00:00 2001 From: Derek Lawless Date: Mon, 25 Jan 2016 22:06:12 -0600 Subject: [PATCH 4/6] Initial test creation --- gapi.auth2/gapi.auth2-tests.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 gapi.auth2/gapi.auth2-tests.ts diff --git a/gapi.auth2/gapi.auth2-tests.ts b/gapi.auth2/gapi.auth2-tests.ts new file mode 100644 index 000000000..cbe6c0fd3 --- /dev/null +++ b/gapi.auth2/gapi.auth2-tests.ts @@ -0,0 +1,29 @@ +/// + +function test_init(){ + var auth = gapi.auth2.init(); +} + +function test_getAuthInstance(){ + gapi.auth2.init(); + var auth = gapi.auth2.getAuthInstance(); +} + +function test_render(){ + var success = (googleUser: gapi.auth2.GoogleUser): void => { + console.log(googleUser); + }; + var failure = (): void => { + console.log('Failure callback'); + }; + + gapi.signin2.render('testId', { + scope: 'some scope', + width: 250, + height: 50, + longtitle: true, + theme: 'dark', + onsuccess: success, + onfailure: failure + }); +} From 3a7cc89e9876a1757645ef165b760d5ae11168b1 Mon Sep 17 00:00:00 2001 From: Derek Lawless Date: Mon, 25 Jan 2016 22:17:27 -0600 Subject: [PATCH 5/6] Fix missing return types --- gapi.auth2/gapi.auth2.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gapi.auth2/gapi.auth2.d.ts b/gapi.auth2/gapi.auth2.d.ts index a133d22e4..29d66848e 100644 --- a/gapi.auth2/gapi.auth2.d.ts +++ b/gapi.auth2/gapi.auth2.d.ts @@ -21,7 +21,7 @@ declare module gapi.auth2 { * Calls the onInit function when the GoogleAuth object is fully initialized, or calls the onFailure function if * initialization fails. */ - then(onInit: () => any, onFailure: (reason: string) => any); + then(onInit: () => any, onFailure: (reason: string) => any): any; /** * Signs in the user with the options specified to gapi.auth2.init(). @@ -280,5 +280,5 @@ declare module gapi.signin2 { * Optional. (default: none) */ app_package_name?: string; - }); + }): void; } From a006f71d8e7b73d0173ac761d47ed2b803bd40b4 Mon Sep 17 00:00:00 2001 From: Derek Lawless Date: Mon, 25 Jan 2016 22:17:44 -0600 Subject: [PATCH 6/6] Add some missing required params to tests --- gapi.auth2/gapi.auth2-tests.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/gapi.auth2/gapi.auth2-tests.ts b/gapi.auth2/gapi.auth2-tests.ts index cbe6c0fd3..5db036652 100644 --- a/gapi.auth2/gapi.auth2-tests.ts +++ b/gapi.auth2/gapi.auth2-tests.ts @@ -1,11 +1,21 @@ /// function test_init(){ - var auth = gapi.auth2.init(); + var auth = gapi.auth2.init({ + client_id: 'my-id', + cookie_policy: 'single_host_origin', + scope: 'https://www.googleapis.com/auth/plus.login', + fetch_basic_profile: true + }); } function test_getAuthInstance(){ - gapi.auth2.init(); + gapi.auth2.init({ + client_id: 'my-id', + cookie_policy: 'single_host_origin', + scope: 'https://www.googleapis.com/auth/plus.login', + fetch_basic_profile: true + }); var auth = gapi.auth2.getAuthInstance(); } @@ -18,7 +28,7 @@ function test_render(){ }; gapi.signin2.render('testId', { - scope: 'some scope', + scope: 'https://www.googleapis.com/auth/plus.login', width: 250, height: 50, longtitle: true,