11import * as AppleAuthentication from 'expo-apple-authentication' ;
2- import { Linking } from 'react-native ' ;
2+ import * as WebBrowser from 'expo-web-browser ' ;
33import { Base64 } from 'js-base64' ;
44
55import Navigation from '../../lib/navigation/appNavigation' ;
6- import { type IItemService , type IOpenOAuth , type IServiceLogin } from './interfaces' ;
6+ import { type IItemService , type IOpenSSOWebView , type IServiceLogin } from './interfaces' ;
77import { random } from '../../lib/methods/helpers' ;
88import { loginOAuthOrSso } from '../../lib/services/connect' ;
9- import { events , logEvent } from '../../lib/methods/helpers/log' ;
9+ import log , { events , logEvent } from '../../lib/methods/helpers/log' ;
10+ import { store } from '../../lib/store/auxStore' ;
11+ import { deepLinkingOpen } from '../../actions/deepLinking' ;
12+ import parseDeepLinking from '../../lib/methods/helpers/parseDeepLinking' ;
1013
1114type TLoginStyle = 'popup' | 'redirect' ;
1215
@@ -16,9 +19,9 @@ export const onPressFacebook = ({ service, server }: IServiceLogin) => {
1619 const endpoint = 'https://m.facebook.com/v2.9/dialog/oauth' ;
1720 const redirect_uri = `${ server } /_oauth/facebook?close` ;
1821 const scope = 'email' ;
19- const state = getOAuthState ( ) ;
22+ const state = getOAuthState ( 'redirect' ) ;
2023 const params = `?client_id=${ clientId } &redirect_uri=${ redirect_uri } &scope=${ scope } &state=${ state } &display=touch` ;
21- openOAuth ( { url : `${ endpoint } ${ params } ` } ) ;
24+ openOAuthSession ( `${ endpoint } ${ params } ` ) ;
2225} ;
2326
2427export const onPressGithub = ( { service, server } : IServiceLogin ) => {
@@ -27,9 +30,9 @@ export const onPressGithub = ({ service, server }: IServiceLogin) => {
2730 const endpoint = `https://github.com/login?client_id=${ clientId } &return_to=${ encodeURIComponent ( '/login/oauth/authorize' ) } ` ;
2831 const redirect_uri = `${ server } /_oauth/github?close` ;
2932 const scope = 'user:email' ;
30- const state = getOAuthState ( ) ;
33+ const state = getOAuthState ( 'redirect' ) ;
3134 const params = `?client_id=${ clientId } &redirect_uri=${ redirect_uri } &scope=${ scope } &state=${ state } ` ;
32- openOAuth ( { url : `${ endpoint } ${ encodeURIComponent ( params ) } ` } ) ;
35+ openOAuthSession ( `${ endpoint } ${ encodeURIComponent ( params ) } ` ) ;
3336} ;
3437
3538export const onPressGitlab = ( { service, server, urlOption } : IServiceLogin ) => {
@@ -39,9 +42,9 @@ export const onPressGitlab = ({ service, server, urlOption }: IServiceLogin) =>
3942 const endpoint = `${ baseURL } /oauth/authorize` ;
4043 const redirect_uri = `${ server } /_oauth/gitlab?close` ;
4144 const scope = 'read_user' ;
42- const state = getOAuthState ( ) ;
45+ const state = getOAuthState ( 'redirect' ) ;
4346 const params = `?client_id=${ clientId } &redirect_uri=${ redirect_uri } &scope=${ scope } &state=${ state } &response_type=code` ;
44- openOAuth ( { url : `${ endpoint } ${ params } ` } ) ;
47+ openOAuthSession ( `${ endpoint } ${ params } ` ) ;
4548} ;
4649
4750export const onPressGoogle = ( { service, server } : IServiceLogin ) => {
@@ -52,7 +55,7 @@ export const onPressGoogle = ({ service, server }: IServiceLogin) => {
5255 const scope = encodeURIComponent ( 'profile email' ) ;
5356 const state = getOAuthState ( 'redirect' ) ;
5457 const params = `?client_id=${ clientId } &redirect_uri=${ redirect_uri } &scope=${ scope } &state=${ state } &response_type=code` ;
55- Linking . openURL ( `${ endpoint } ${ params } ` ) ;
58+ openOAuthSession ( `${ endpoint } ${ params } ` ) ;
5659} ;
5760
5861export const onPressLinkedin = ( { service, server } : IServiceLogin ) => {
@@ -61,26 +64,26 @@ export const onPressLinkedin = ({ service, server }: IServiceLogin) => {
6164 const endpoint = 'https://www.linkedin.com/oauth/v2/authorization' ;
6265 const redirect_uri = `${ server } /_oauth/linkedin?close` ;
6366 const scope = 'r_liteprofile,r_emailaddress' ;
64- const state = getOAuthState ( ) ;
67+ const state = getOAuthState ( 'redirect' ) ;
6568 const params = `?client_id=${ clientId } &redirect_uri=${ redirect_uri } &scope=${ scope } &state=${ state } &response_type=code` ;
66- openOAuth ( { url : `${ endpoint } ${ params } ` } ) ;
69+ openOAuthSession ( `${ endpoint } ${ params } ` ) ;
6770} ;
6871
6972export const onPressMeteor = ( { service, server } : IServiceLogin ) => {
7073 logEvent ( events . ENTER_WITH_METEOR ) ;
7174 const { clientId } = service ;
7275 const endpoint = 'https://www.meteor.com/oauth2/authorize' ;
7376 const redirect_uri = `${ server } /_oauth/meteor-developer` ;
74- const state = getOAuthState ( ) ;
77+ const state = getOAuthState ( 'redirect' ) ;
7578 const params = `?client_id=${ clientId } &redirect_uri=${ redirect_uri } &state=${ state } &response_type=code` ;
76- openOAuth ( { url : `${ endpoint } ${ params } ` } ) ;
79+ openOAuthSession ( `${ endpoint } ${ params } ` ) ;
7780} ;
7881
7982export const onPressTwitter = ( { server } : IServiceLogin ) => {
8083 logEvent ( events . ENTER_WITH_TWITTER ) ;
81- const state = getOAuthState ( ) ;
84+ const state = getOAuthState ( 'redirect' ) ;
8285 const url = `${ server } /_oauth/twitter/?requestTokenAndRedirect=true&state=${ state } ` ;
83- openOAuth ( { url } ) ;
86+ openOAuthSession ( url ) ;
8487} ;
8588
8689export const onPressWordpress = ( { service, server } : IServiceLogin ) => {
@@ -89,24 +92,24 @@ export const onPressWordpress = ({ service, server }: IServiceLogin) => {
8992 const endpoint = `${ serverURL } /oauth/authorize` ;
9093 const redirect_uri = `${ server } /_oauth/wordpress?close` ;
9194 const scope = 'openid' ;
92- const state = getOAuthState ( ) ;
95+ const state = getOAuthState ( 'redirect' ) ;
9396 const params = `?client_id=${ clientId } &redirect_uri=${ redirect_uri } &scope=${ scope } &state=${ state } &response_type=code` ;
94- openOAuth ( { url : `${ endpoint } ${ params } ` } ) ;
97+ openOAuthSession ( `${ endpoint } ${ params } ` ) ;
9598} ;
9699
97100export const onPressCustomOAuth = ( { loginService, server } : { loginService : IItemService ; server : string } ) => {
98101 logEvent ( events . ENTER_WITH_CUSTOM_OAUTH ) ;
99102 const { serverURL, authorizePath, clientId, scope, service } = loginService ;
100103 const redirectUri = `${ server } /_oauth/${ service } ` ;
101- const state = getOAuthState ( ) ;
104+ const state = getOAuthState ( 'redirect' ) ;
102105 const separator = authorizePath . indexOf ( '?' ) !== - 1 ? '&' : '?' ;
103106 const params = `${ separator } client_id=${ clientId } &redirect_uri=${ encodeURIComponent (
104107 redirectUri
105108 ) } &response_type=code&state=${ state } &scope=${ encodeURIComponent ( scope ) } `;
106109 const domain = `${ serverURL } ` ;
107110 const absolutePath = `${ authorizePath } ${ params } ` ;
108111 const url = absolutePath . includes ( domain ) ? absolutePath : domain + absolutePath ;
109- openOAuth ( { url } ) ;
112+ openOAuthSession ( url ) ;
110113} ;
111114
112115export const onPressSaml = ( { loginService, server } : { loginService : IItemService ; server : string } ) => {
@@ -115,14 +118,14 @@ export const onPressSaml = ({ loginService, server }: { loginService: IItemServi
115118 const { provider } = clientConfig ;
116119 const ssoToken = random ( 17 ) ;
117120 const url = `${ server } /_saml/authorize/${ provider } /${ ssoToken } ` ;
118- openOAuth ( { url, ssoToken, authType : 'saml' } ) ;
121+ openSSOWebView ( { url, ssoToken, authType : 'saml' } ) ;
119122} ;
120123
121124export const onPressCas = ( { casLoginUrl, server } : { casLoginUrl : string ; server : string } ) => {
122125 logEvent ( events . ENTER_WITH_CAS ) ;
123126 const ssoToken = random ( 17 ) ;
124127 const url = `${ casLoginUrl } ?service=${ server } /_cas/${ ssoToken } ` ;
125- openOAuth ( { url, ssoToken, authType : 'cas' } ) ;
128+ openSSOWebView ( { url, ssoToken, authType : 'cas' } ) ;
126129} ;
127130
128131export const onPressAppleLogin = async ( ) => {
@@ -140,6 +143,22 @@ export const onPressAppleLogin = async () => {
140143 }
141144} ;
142145
146+ const OAUTH_REDIRECT_URL = 'rocketchat://auth' ;
147+
148+ const openOAuthSession = async ( url : string ) => {
149+ try {
150+ const result = await WebBrowser . openAuthSessionAsync ( url , OAUTH_REDIRECT_URL ) ;
151+ if ( result . type === 'success' && 'url' in result && result . url ) {
152+ const parsed = parseDeepLinking ( result . url ) ;
153+ if ( parsed ) {
154+ store . dispatch ( deepLinkingOpen ( parsed ) ) ;
155+ }
156+ }
157+ } catch ( e ) {
158+ log ( e ) ;
159+ }
160+ } ;
161+
143162const getOAuthState = ( loginStyle : TLoginStyle = 'popup' ) => {
144163 const credentialToken = random ( 43 ) ;
145164 let obj : {
@@ -157,6 +176,6 @@ const getOAuthState = (loginStyle: TLoginStyle = 'popup') => {
157176 return Base64 . encodeURI ( JSON . stringify ( obj ) ) ;
158177} ;
159178
160- const openOAuth = ( { url, ssoToken, authType = 'oauth' } : IOpenOAuth ) => {
179+ const openSSOWebView = ( { url, ssoToken, authType } : IOpenSSOWebView ) => {
161180 Navigation . navigate ( 'AuthenticationWebView' , { url, authType, ssoToken } ) ;
162181} ;
0 commit comments