Skip to content

Instantly share code, notes, and snippets.

@alnutile
Created June 4, 2017 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alnutile/23a87e96d7c6f6ca0658ed8ca812c194 to your computer and use it in GitHub Desktop.
Save alnutile/23a87e96d7c6f6ca0658ed8ca812c194 to your computer and use it in GitHub Desktop.
<style scoped>
.action-link {
cursor: pointer;
}
.m-b-none {
margin-bottom: 0;
}
</style>
<template>
<div>
<div class="panel panel-default">
<div class="panel-heading">
<div style="display: flex; justify-content: space-between; align-items: center;">
<span>
OAuth Clients
</span>
<a class="action-link" @click="showCreateClientForm">
Create New Client
</a>
</div>
</div>
<div class="panel-body">
<!-- Current Clients -->
<p class="m-b-none" v-if="clients.length === 0">
You have not created any OAuth clients.
</p>
<div class="alert alert-info">
You can use the Bearer Token to talk to the API or<br>
Connect as seen in this example.<br>
Learn more <a href="https://laravel.com/docs/5.3/passport#client-credentials-grant-tokens" target="_blank">here</a>
</div>
<div class="alert alert-warning">
<strong>example request to get token from secret</strong><br><br>
<code>
$guzzle = new GuzzleHttp\Client;<br>
$response = $guzzle->post('http://THIS_URL/oauth/token', [<br>
&nbsp;&nbsp;'form_params' => [<br>
&nbsp;&nbsp;'grant_type' => 'client_credentials',<br>
&nbsp;&nbsp;'client_id' => 'client-id',<br>
&nbsp;&nbsp;'client_secret' => 'client-secret',<br>
&nbsp;&nbsp;'scope' => 'your-scope',<br>
&nbsp;],<br>
]);<br>
<br>
echo json_decode((string) $response->getBody(), true);<br>
</code>
<br>
<strong>--or--</strong>
<br>
<code>
curl -k -X POST \<br>
https://APP_URL/oauth/token \<br>
-H 'accept: application/json' \<br>
-H 'cache-control: no-cache' \<br>
-H 'content-type: application/x-www-form-urlencoded' \<br>
-d 'grant_type=client_credentials&client_id=1&client_secret=BLhW0TQtCsKqzCmmd1CbPuz7risux2gVQ62LM29A'<br>
</code>
</div>
<table class="table table-borderless m-b-none" v-if="clients.length > 0">
<thead>
<tr>
<th>Client ID</th>
<th>Name</th>
<th>Secret</th>
<th>Bearer Token</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr v-for="client in clients">
<!-- ID -->
<td style="vertical-align: middle;">
{{ client.id }}
</td>
<!-- Name -->
<td style="vertical-align: middle;">
{{ client.name }}
</td>
<!-- Secret -->
<td style="vertical-align: middle;">
<code>{{ client.secret }}</code>
</td>
<td style="vertical-align: middle;">
<a class="action-link text-danger" @click="clientCreds(client)">
Create Machine 2 Machine Token
<i class="fa fa-gears" v-bind:class="{ 'fa-spin': getting_token }"></i>
</a>
</td>
<!-- Edit Button -->
<td style="vertical-align: middle;">
<a class="action-link" @click="edit(client)">
Edit
</a>
</td>
<!-- Delete Button -->
<td style="vertical-align: middle;">
<a class="action-link text-danger" @click="destroy(client)">
Delete
</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- Show bearer -->
<div class="modal fade" id="modal-show-bearer" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">
Bearer Token for App
</h4>
</div>
<div class="modal-body">
<p>Use this token to connect to API</p>
<pre>{{ bearer_token }}</pre>
<button class="btn btn-link" id="clipbutton" @click="copyText()">
<i class="fa fa-clipboard"></i>&nbsp;</button>
<input type="hidden" id="clipvalue" value="foo">
<p>Example Usage</p>
<code>
curl -k -X GET https://APP_URL/api/user
--header "Accept: application/json" --header "Authorization: Bearer [TOKEN_SEEN_ABOVE]"
</code>
</div>
<!-- Modal Actions -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Create Client Modal -->
<div class="modal fade" id="modal-create-client" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button " class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">
Create Client
</h4>
</div>
<div class="modal-body">
<!-- Form Errors -->
<div class="alert alert-danger" v-if="createForm.errors.length > 0">
<p><strong>Whoops!</strong> Something went wrong!</p>
<br>
<ul>
<li v-for="error in createForm.errors">
{{ error }}
</li>
</ul>
</div>
<!-- Create Client Form -->
<form class="form-horizontal" role="form">
<!-- Name -->
<div class="form-group">
<label class="col-md-3 control-label">Name</label>
<div class="col-md-7">
<input id="create-client-name" type="text" class="form-control"
@keyup.enter="store" v-model="createForm.name">
<span class="help-block">
Something your users will recognize and trust.
</span>
</div>
</div>
<!-- Redirect URL -->
<div class="form-group">
<label class="col-md-3 control-label">Redirect URL</label>
<div class="col-md-7">
<input type="text" class="form-control" name="redirect"
@keyup.enter="store" v-model="createForm.redirect">
<span class="help-block">
Your application's authorization callback URL.
</span>
</div>
</div>
</form>
</div>
<!-- Modal Actions -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" @click="store">
Create
</button>
</div>
</div>
</div>
</div>
<!-- Edit Client Modal -->
<div class="modal fade" id="modal-edit-client" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button " class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title">
Edit Client
</h4>
</div>
<div class="modal-body">
<!-- Form Errors -->
<div class="alert alert-danger" v-if="editForm.errors.length > 0">
<p><strong>Whoops!</strong> Something went wrong!</p>
<br>
<ul>
<li v-for="error in editForm.errors">
{{ error }}
</li>
</ul>
</div>
<!-- Edit Client Form -->
<form class="form-horizontal" role="form">
<!-- Name -->
<div class="form-group">
<label class="col-md-3 control-label">Name</label>
<div class="col-md-7">
<input id="edit-client-name" type="text" class="form-control"
@keyup.enter="update" v-model="editForm.name">
<span class="help-block">
Something your users will recognize and trust.
</span>
</div>
</div>
<!-- Redirect URL -->
<div class="form-group">
<label class="col-md-3 control-label">Redirect URL</label>
<div class="col-md-7">
<input type="text" class="form-control" name="redirect"
@keyup.enter="update" v-model="editForm.redirect">
<span class="help-block">
Your application's authorization callback URL.
</span>
</div>
</div>
</form>
</div>
<!-- Modal Actions -->
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" @click="update">
Save Changes
</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
/*
* The component's data.
*/
data() {
return {
clients: [],
bearer_token: null,
getting_token: false,
createForm: {
errors: [],
name: '',
redirect: ''
},
editForm: {
errors: [],
name: '',
redirect: ''
}
};
},
/**
* Prepare the component (Vue 1.x).
*/
ready() {
this.prepareComponent();
},
/**
* Prepare the component (Vue 2.x).
*/
mounted() {
this.prepareComponent();
},
methods: {
copyText() {
window.document.querySelector("#clipvalue").select();
// Copy to the clipboard
window.document.execCommand('copy');
},
/**
* Prepare the component.
*/
prepareComponent() {
this.getClients();
$('#modal-create-client').on('shown.bs.modal', () => {
$('#create-client-name').focus();
});
$('#modal-edit-client').on('shown.bs.modal', () => {
$('#edit-client-name').focus();
});
},
/**
* Get all of the OAuth clients for the user.
*/
getClients() {
axios.get('/oauth/clients')
.then(response => {
this.clients = response.data;
});
},
getTokens() {
axios.get('/clients/tokens')
.then(response => {
this.tokens = response.data;
});
},
/**
* Show the form for creating new clients.
*/
showCreateClientForm() {
$('#modal-create-client').modal('show');
},
/**
* Create a new OAuth client for the user.
*/
store() {
this.persistClient(
'post', '/oauth/clients',
this.createForm, '#modal-create-client'
);
},
/**
* Edit the given client.
*/
edit(client) {
this.editForm.id = client.id;
this.editForm.name = client.name;
this.editForm.redirect = client.redirect;
$('#modal-edit-client').modal('show');
},
/**
* Update the client being edited.
*/
update() {
this.persistClient(
'put', '/oauth/clients/' + this.editForm.id,
this.editForm, '#modal-edit-client'
);
},
/**
* Persist the client to storage using the given form.
*/
persistClient(method, uri, form, modal) {
form.errors = [];
axios[method](uri, form)
.then(response => {
this.getClients();
form.name = '';
form.redirect = '';
form.errors = [];
$(modal).modal('hide');
})
.catch(response => {
if (typeof response.data === 'object') {
form.errors = _.flatten(_.toArray(response.data));
} else {
form.errors = ['Something went wrong. Please try again.'];
}
});
},
clientCreds(client) {
this.getting_token = true;
axios.post('/oauth/token', {
"grant_type": "client_credentials",
"client_id": client.id,
"client_secret": client.secret
}
).then(response => {
this.bearer_token = response.data.access_token;
$('#modal-show-bearer').modal('show');
this.getting_token = false;
});
},
/**
* Destroy the given client.
*/
destroy(client) {
axios.delete('/oauth/clients/' + client.id)
.then(response => {
this.getClients();
});
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment