Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Adding all the basic logic for refreshing token #556

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 8 additions & 3 deletions client/components/RepositoryConfigValidation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Result extends React.Component {
}
}

export default function RepositoryConfigValidation({validation, onValidate}) {
export default function RepositoryConfigValidation({validation, onValidate, refreshingToken = true, refreshToken}) {
const placeholder = <div style={{marginTop: '1em'}} />
let result = placeholder
if (validationFinished(validation.status)) {
Expand All @@ -90,7 +90,7 @@ export default function RepositoryConfigValidation({validation, onValidate}) {
} else if (validation.error) {
result = <Alert style={{marginTop: '1em'}} bsStyle='danger'>{validation.error.status} {validation.error.title}</Alert>
}
return <div>
return (<div>
<Button onClick={onValidate}>
<span className={classes('fa', 'fa-fw', {
'fa-spin': validation.status === Status.PENDING,
Expand All @@ -99,10 +99,15 @@ export default function RepositoryConfigValidation({validation, onValidate}) {
})}/>
Validate Zappr configuration
</Button>
<Button onClick={refreshToken}>
<span className={classes('fa', 'fa-fw', 'fa-refresh')}></span>
Refresh OAuth Token
</Button>
{result}
</div>
</div>);
}


RepositoryConfigValidation.propTypes = {
validation: PropTypes.shape({
status: PropTypes.symbol,
Expand Down
9 changes: 7 additions & 2 deletions client/containers/RepositoryDetail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ class RepositoryDetail extends Component {
this.props.requestConfigValidation(repo)
}

shouldRefreshToken() {
console.log("refresh Token");
// this.props.requestNewToken();
}

render() {
if (!this.props.repository.full_name) return null

const {repository, checks, validations} = this.props
const header = (<h2>{repository.full_name}</h2>)

Expand All @@ -60,7 +64,8 @@ class RepositoryDetail extends Component {
<Col md={12}>
<ConfigValidation
validation={validations[repository.full_name]}
onValidate={this.onValidateConfig.bind(this, repository)}/>
onValidate={this.onValidateConfig.bind(this, repository)}
refreshToken={this.shouldRefreshToken()}/>
</Col>
<Col md={12}>
{CHECK_TYPES
Expand Down
22 changes: 22 additions & 0 deletions client/service/RepoService.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,26 @@ export default class RepoService extends Service {
})
})
}

static refreshTokens(repoId) {
return fetch(Service.url(`/api/repos/${repoId}/refreshTokens`), {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
credentials: 'same-origin',
body: {}
}).then(response => {
return new Promise((resolve, reject) => {
return response.json().then(json => {
if(response.ok) {
resolve(json)
} else {
reject(json)
}
})
});
})
}
}
6 changes: 3 additions & 3 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ SESSION_SECRET: secret

# Override Github client id and secret with your
# own values as defined in your Github account.
GITHUB_CLIENT_ID: clientId
GITHUB_CLIENT_SECRET: clientSecret
GITHUB_CLIENT_ID: 72b6d96339bf1c5d5257
GITHUB_CLIENT_SECRET: b754189140f9c0e17a9c44e9feaf1d0764ed9a06
GITHUB_HOOK_SECRET: captainHook
GITHUB_ACCESS_LEVELS:
minimal:
Expand All @@ -26,7 +26,7 @@ GITHUB_API_URL: https://api.github.com

# Address of the web application host
# that ZAPPR is going to run on.
HOST_ADDR: http://localhost:8080
#HOST_ADDR: http://localhost:8080
APP_PORT: 3000
METRICS_PORT: 3003
METRICS_ENABLED: true
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ services:
- DB_HOST=database
- NODE_ENV=development
- HOST_ADDR=http://$DM_IP:3000
- GITHUB_CLIENT_ID
- GITHUB_CLIENT_SECRET
- GITHUB_CLIENT_ID=72b6d96339bf1c5d5257
- GITHUB_CLIENT_SECRET=b754189140f9c0e17a9c44e9feaf1d0764ed9a06
15 changes: 10 additions & 5 deletions server/handler/CheckHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ export class CheckHandler {
}
}

onRefreshTokens(repoIds, token) {
// noop for now
// implement this when we fucked up
return Promise.resolve()
async onRefreshTokens(repoIds, token, user) {
debug(`refreshing token for all checks for repo ${repoId} w/ token ${token ? token.substr(0, 4) : 'NONE'} by user ${user} `)
return await Check.update({ token: token },
{
where: {
repositoryId: repoId,
},
returning: true
});
}

/**
Expand All @@ -71,7 +76,7 @@ export class CheckHandler {
repositoryId: repoId,
type
}
})
});
} catch (e) {
throw new CheckHandlerError(DATABASE_ERROR, {type, repository: repoId})
}
Expand Down
1 change: 1 addition & 0 deletions server/handler/UserHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class UserHandler {
onChangeLevel(userId, access_level) {
return User.update({access_level}, {where: {id: userId}})
}

}

export default new UserHandler()
12 changes: 12 additions & 0 deletions server/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ export function repo(router) {
ctx.throw(404, e)
}
})
.post('/api/repos/:id/refreshTokens', requireAuth, async(ctx) => {
try{
const user = ctx.req.user;
const token = user.accessToken
const id = parseInt(ctx.params.id)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to repoId

const updatedTokens = await checkHandler.onRefreshTokens(id, token, user);
ctx.body = updatedTokens;
ctx.response.status = 204;
} catch(e) {
ctx.throw(404, e)
}
})
.get('/api/repos/:id/zapprfile', requireAuth, async(ctx) => {
const user = ctx.req.user
const id = parseInt(ctx.params.id, 10)
Expand Down