Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login from code #1301

Merged
merged 5 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/sensenet/cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"identityServer": "https://is.demo.sensenet.com",
"users": {
"admin": {
"clientId": "businesscat",
"clientId": "KxkgOTAxLLCicUx7",
"clientSecret": "",
"id": "/Root/IMS/Public('businesscat')"
},
Expand Down
37 changes: 9 additions & 28 deletions apps/sensenet/cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'cypress-file-upload'
import { codeLogin } from '@sensenet/authentication-oidc-react'

// ***********************************************
// This example commands.js shows you how to
// create various custom commands and overwrite
Expand Down Expand Up @@ -35,34 +37,13 @@ Cypress.on('uncaught:exception', (err) => {

Cypress.Commands.add('login', (userType = 'admin') => {
const user = Cypress.env('users')[userType]

const configuration = {
client_id: user.clientId,
client_secret: Cypress.env(`secret_${userType}`) || user.clientSecret,
grant_type: 'client_credentials',
scope: encodeURIComponent('sensenet'),
}

const requestBody = Object.keys(configuration).reduce((acc, current, idx) => {
return `${acc}${current}=${configuration[current]}${idx === Object.keys(configuration).length - 1 ? '' : '&'}`
}, '')

cy.request({
url: `${Cypress.env('identityServer')}/connect/token`,
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: requestBody,
}).then((resp) => {
const oidcUser = resp.body

oidcUser.profile = {
sub: user.id,
}

window.sessionStorage.setItem(
`oidc.user:${Cypress.env('identityServer')}:11V28Add7IaP1iFw`,
JSON.stringify(oidcUser),
)
codeLogin({
clientId: user.clientId,
clientSecret: Cypress.env(`secret_${userType}`) || user.clientSecret,
identityServerUrl: Cypress.env('identityServer'),
appId: '11V28Add7IaP1iFw',
userId: user.id,
fetchMethod: (url, options) => cy.request({ url, ...options }),
})
})

Expand Down
2 changes: 2 additions & 0 deletions examples/sn-react-browser/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
SKIP_PREFLIGHT_CHECK=true
FAST_REFRESH=false
REACT_APP_CLIENT_ID=KxkgOTAxLLCicUx7
REACT_APP_CLIENT_SECRET=
37 changes: 16 additions & 21 deletions examples/sn-react-browser/src/components/app-providers.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
import { AuthenticationProvider, useOidcAuthentication } from '@sensenet/authentication-oidc-react'
import { codeLogin, CodeLoginResponse } from '@sensenet/authentication-oidc-react'
import { Repository } from '@sensenet/client-core'
import { RepositoryContext } from '@sensenet/hooks-react'
import React, { PropsWithChildren } from 'react'
import { BrowserRouter, useHistory } from 'react-router-dom'
import React, { PropsWithChildren, useEffect, useState } from 'react'
import { BrowserRouter } from 'react-router-dom'
import { configuration, repositoryUrl } from '../configuration'
import { LoginForm } from './login-form'
import { FullScreenLoader } from './full-screen-loader'

export function AppProviders({ children }: PropsWithChildren<{}>) {
return (
<BrowserRouter>
<AuthProvider>
<RepositoryProvider>{children}</RepositoryProvider>
</AuthProvider>
<RepositoryProvider>{children}</RepositoryProvider>
</BrowserRouter>
)
}

export const AuthProvider = ({ children }: PropsWithChildren<{}>) => {
const history = useHistory()

return (
<AuthenticationProvider configuration={configuration} history={history}>
{children}
</AuthenticationProvider>
)
}

export const RepositoryProvider = ({ children }: PropsWithChildren<{}>) => {
const { oidcUser } = useOidcAuthentication()
const [authData, setAuthData] = useState<CodeLoginResponse>()

useEffect(() => {
;(async () => {
const response = await codeLogin(configuration)
setAuthData(response)
})()
}, [])

if (!oidcUser) {
return <LoginForm />
if (!authData) {
return <FullScreenLoader />
}

return (
<RepositoryContext.Provider value={new Repository({ repositoryUrl, token: oidcUser.access_token })}>
<RepositoryContext.Provider value={new Repository({ repositoryUrl, token: authData.access_token })}>
{children}
</RepositoryContext.Provider>
)
Expand Down
23 changes: 0 additions & 23 deletions examples/sn-react-browser/src/components/login-form.tsx

This file was deleted.

7 changes: 1 addition & 6 deletions examples/sn-react-browser/src/components/navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useOidcAuthentication } from '@sensenet/authentication-oidc-react'
import { AppBar, Button, Toolbar, Typography } from '@material-ui/core'
import { AppBar, Toolbar, Typography } from '@material-ui/core'
import { createStyles, makeStyles } from '@material-ui/core/styles'
import React from 'react'

Expand All @@ -17,7 +16,6 @@ const useStyles = makeStyles(() =>
* Navbar component
*/
export const NavBarComponent: React.FunctionComponent = () => {
const { logout } = useOidcAuthentication()
const classes = useStyles()

return (
Expand All @@ -27,9 +25,6 @@ export const NavBarComponent: React.FunctionComponent = () => {
<Typography variant="h6" className={classes.title}>
Document Browser
</Typography>
<Button color="inherit" onClick={logout}>
Log out
</Button>
</Toolbar>
</AppBar>
</div>
Expand Down
18 changes: 7 additions & 11 deletions examples/sn-react-browser/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { UserManagerSettings } from '@sensenet/authentication-oidc-react'
import { CodeLoginParams } from '@sensenet/authentication-oidc-react'

export const repositoryUrl = 'https://dev.demo.sensenet.com/'
export const repositoryUrl = 'https://dev.demo.sensenet.com'

export const configuration: UserManagerSettings = {
client_id: '7cYLChuhJxyGb7BS', //externalSPA clientId for dev.demo.sensenet.com
redirect_uri: `${window.location.origin}/authentication/callback`,
response_type: 'code',
post_logout_redirect_uri: `${window.location.origin}/`,
scope: 'openid profile sensenet',
authority: 'https://is.demo.sensenet.com/',
silent_redirect_uri: `${window.location.origin}/authentication/silent_callback`,
extraQueryParams: { snrepo: repositoryUrl },
export const configuration: CodeLoginParams = {
clientId: process.env.REACT_APP_CLIENT_ID ?? '', // businesscat clientId for dev.demo.sensenet.com
clientSecret: process.env.REACT_APP_CLIENT_SECRET ?? '',
identityServerUrl: 'https://is.demo.sensenet.com',
appId: '7cYLChuhJxyGb7BS', //externalSPA clientId for dev.demo.sensenet.com
}
28 changes: 0 additions & 28 deletions examples/sn-react-browser/test/navbar.test.tsx

This file was deleted.

2 changes: 2 additions & 0 deletions examples/sn-react-calendar/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
SKIP_PREFLIGHT_CHECK=true
FAST_REFRESH=false
REACT_APP_CLIENT_ID=KxkgOTAxLLCicUx7
REACT_APP_CLIENT_SECRET=
37 changes: 16 additions & 21 deletions examples/sn-react-calendar/src/components/app-providers.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
import { AuthenticationProvider, useOidcAuthentication } from '@sensenet/authentication-oidc-react'
import { codeLogin, CodeLoginResponse } from '@sensenet/authentication-oidc-react'
import { Repository } from '@sensenet/client-core'
import { RepositoryContext } from '@sensenet/hooks-react'
import React, { PropsWithChildren } from 'react'
import { BrowserRouter, useHistory } from 'react-router-dom'
import React, { PropsWithChildren, useEffect, useState } from 'react'
import { BrowserRouter } from 'react-router-dom'
import { configuration, repositoryUrl } from '../configuration'
import { LoginForm } from './login-form'
import { FullScreenLoader } from './full-screen-loader'

export function AppProviders({ children }: PropsWithChildren<{}>) {
return (
<BrowserRouter>
<AuthProvider>
<RepositoryProvider>{children}</RepositoryProvider>
</AuthProvider>
<RepositoryProvider>{children}</RepositoryProvider>
</BrowserRouter>
)
}

export const AuthProvider = ({ children }: PropsWithChildren<{}>) => {
const history = useHistory()

return (
<AuthenticationProvider configuration={configuration} history={history}>
{children}
</AuthenticationProvider>
)
}

export const RepositoryProvider = ({ children }: PropsWithChildren<{}>) => {
const { oidcUser } = useOidcAuthentication()
const [authData, setAuthData] = useState<CodeLoginResponse>()

useEffect(() => {
;(async () => {
const response = await codeLogin(configuration)
setAuthData(response)
})()
}, [])

if (!oidcUser) {
return <LoginForm />
if (!authData) {
return <FullScreenLoader />
}

return (
<RepositoryContext.Provider value={new Repository({ repositoryUrl, token: oidcUser.access_token })}>
<RepositoryContext.Provider value={new Repository({ repositoryUrl, token: authData.access_token })}>
{children}
</RepositoryContext.Provider>
)
Expand Down
23 changes: 0 additions & 23 deletions examples/sn-react-calendar/src/components/login-form.tsx

This file was deleted.

7 changes: 1 addition & 6 deletions examples/sn-react-calendar/src/components/navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useOidcAuthentication } from '@sensenet/authentication-oidc-react'
import { AppBar, Button, Toolbar, Typography } from '@material-ui/core'
import { AppBar, Toolbar, Typography } from '@material-ui/core'
import { createStyles, makeStyles, Theme } from '@material-ui/core/styles'
import React from 'react'

Expand All @@ -17,7 +16,6 @@ const useStyles = makeStyles((theme: Theme) =>
* Navbar component
*/
export const NavBarComponent: React.FunctionComponent = () => {
const { logout } = useOidcAuthentication()
const classes = useStyles()

return (
Expand All @@ -27,9 +25,6 @@ export const NavBarComponent: React.FunctionComponent = () => {
<Typography variant="h6" className={classes.title}>
Calendar
</Typography>
<Button color="inherit" onClick={logout}>
Log out
</Button>
</Toolbar>
</AppBar>
</div>
Expand Down
18 changes: 7 additions & 11 deletions examples/sn-react-calendar/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { UserManagerSettings } from '@sensenet/authentication-oidc-react'
import { CodeLoginParams } from '@sensenet/authentication-oidc-react'

export const repositoryUrl = 'https://dev.demo.sensenet.com/'
export const repositoryUrl = 'https://dev.demo.sensenet.com'

export const configuration: UserManagerSettings = {
client_id: '7cYLChuhJxyGb7BS', //externalSPA clientId for dev.demo.sensenet.com
redirect_uri: `${window.location.origin}/authentication/callback`,
response_type: 'code',
post_logout_redirect_uri: `${window.location.origin}/`,
scope: 'openid profile sensenet',
authority: 'https://is.demo.sensenet.com/',
silent_redirect_uri: `${window.location.origin}/authentication/silent_callback`,
extraQueryParams: { snrepo: repositoryUrl },
export const configuration: CodeLoginParams = {
clientId: process.env.REACT_APP_CLIENT_ID ?? '', // businesscat clientId for dev.demo.sensenet.com
clientSecret: process.env.REACT_APP_CLIENT_SECRET ?? '',
identityServerUrl: 'https://is.demo.sensenet.com',
appId: '7cYLChuhJxyGb7BS', //externalSPA clientId for dev.demo.sensenet.com
}
2 changes: 2 additions & 0 deletions examples/sn-react-imagegallery/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
SKIP_PREFLIGHT_CHECK=true
FAST_REFRESH=false
REACT_APP_CLIENT_ID=KxkgOTAxLLCicUx7
REACT_APP_CLIENT_SECRET=
Loading