Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0
- https://github.com/yiisoft/yii2/issues/6631
- function send(){
- var url = "http://www.server.org/app/api/oauth2/rest/token";
- var data = {
- 'grant_type':'password',
- 'username':'user',
- 'password':'pass',
- 'client_id':'clientid',
- 'client_secret':'clientsecret',
- };
- $.ajax({
- type: "POST",
- url: url,
- data: data,
- success:function(data){
- console.log(data);
- token = data.access_token;
- },
- })
- };
- function createuser(){
- var url = "http://www.server.org/app/api/v1/users/create";
- var data = {
- 'callback':'asdf',
- 'username': 'user',
- 'password':'pass',
- 'first_name':'name',
- 'last_name':'lastname'
- };
- $.ajax({
- type: "POST",
- url: url,
- data: data,
- beforeSend: function (xhr) {
- xhr.setRequestHeader('Authorization', 'Bearer ' + token);
- },
- success:function(r){
- console.log(r);
- },
- });
- }
- <?php
- namespace appapimodulesv1controllers;
- use Yii;
- use appmodelsOauthUsers;
- use yiirestActiveController;
- use yiiwebResponse;
- use yiihelpersArrayHelper;
- use yiifiltersauthHttpBearerAuth;
- use yiifiltersauthQueryParamAuth;
- use filshyii2oauth2serverfiltersErrorToExceptionFilter;
- use filshyii2oauth2serverfiltersauthCompositeAuth;
- class UsersController extends yiiwebController
- {
- public function behaviors()
- {
- return ArrayHelper::merge(parent::behaviors(), [
- 'authenticator' => [
- 'class' => CompositeAuth::className(),
- 'authMethods' => [
- ['class' => HttpBearerAuth::className()],
- ['class' => QueryParamAuth::className(), 'tokenParam' => 'accessToken'],
- ]
- ],
- 'exceptionFilter' => [
- 'class' => ErrorToExceptionFilter::className()
- ],
- 'class' => yiifiltersContentNegotiator::className(),
- ]);
- }
- /**
- * Creates a new model.
- * If creation is successful, the browser will be redirected to the 'view' page.
- * @return mixed
- */
- public function actionCreate()
- {
- $model = new OauthUsers;
- try {
- if ($model->load($_POST) && $model->save()) {
- return $this->redirect(Url::previous());
- } elseif (!Yii::$app->request->isPost) {
- $model->load($_GET);
- }
- } catch (Exception $e) {
- $msg = (isset($e->errorInfo[2]))?$e->errorInfo[2]:$e->getMessage();
- $model->addError('_exception', $msg);
- }
- return "true";
- }
- }
- 'oauth2' => [
- 'class' => 'filshyii2oauth2serverModule',
- 'tokenParamName' => 'accessToken',
- 'tokenAccessLifetime' => 3600 * 24,
- 'storageMap' => [
- 'user_credentials' => 'appmodelsOauthUsers',
- ],
- 'grantTypes' => [
- 'user_credentials' => [
- 'class' => 'OAuth2GrantTypeUserCredentials',
- ],
- 'refresh_token' => [
- 'class' => 'OAuth2GrantTypeRefreshToken',
- 'always_issue_new_refresh_token' => true
- ]
- ]
- ]
- <?php
- namespace appmodels;
- use Yii;
- use appmodelsbaseOauthUsers as BaseOauthUsers;
- /**
- * This is the model class for table "oauth_users".
- */
- class OauthUsers extends BaseOauthUsers
- implements yiiwebIdentityInterface,OAuth2StorageUserCredentialsInterface
- {
- /**
- * @inheritdoc
- */
- public static function findIdentity($id) {
- $dbUser = OauthUsers::find()
- ->where([
- "id" => $id
- ])
- ->one();
- if (!count($dbUser)) {
- return null;
- }
- return new static($dbUser);
- }
- /**
- * @inheritdoc
- */
- public static function findIdentityByAccessToken($token, $userType = null) {
- $at = OauthAccessTokens::find()
- ->where(["access_token" => $token])
- ->one();
- $dbUser = OauthUsers::find()
- ->where(["id" => $at->user_id])
- ->one();
- if (!count($dbUser)) {
- return null;
- }
- return new static($dbUser);
- }
- /**
- * Implemented for Oauth2 Interface
- */
- public function checkUserCredentials($username, $password)
- {
- $user = static::findByUsername($username);
- if (empty($user)) {
- return false;
- }
- return $user->validatePassword($password);
- }
- /**
- * Implemented for Oauth2 Interface
- */
- public function getUserDetails($username)
- {
- $user = static::findByUsername($username);
- return ['user_id' => $user->getId()];
- }
- /**
- * Finds user by username
- *
- * @param string $username
- * @return static|null
- */
- public static function findByUsername($username) {
- $dbUser = OauthUsers::find()
- ->where([
- "username" => $username
- ])
- ->one();
- if (!count($dbUser)) {
- return null;
- }
- return new static($dbUser);
- }
- /**
- * @inheritdoc
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * @inheritdoc
- */
- public function getAuthKey()
- {
- return $this->authKey;
- }
- /**
- * @inheritdoc
- */
- public function validateAuthKey($authKey)
- {
- return $this->authKey === $authKey;
- }
- /**
- * Validates password
- *
- * @param string $password password to validate
- * @return boolean if password provided is valid for current user
- */
- public function validatePassword($password)
- {
- return $this->password === $password;
- }
- }
- function createuser(){
- var url = "http://www.server.org/app/api/v1/users/create";
- var data = {
- 'callback':'cb',
- 'username': 'user',
- 'password':'pass',
- 'first_name':'name',
- 'last_name':'lastname'
- };
- $.ajax({
- type: "POST",
- url: url,
- data: data,
- beforeSend: function (xhr) {
- xhr.setRequestHeader('Authorization', 'Bearer ' + token);
- },
- success:function(r){
- console.log(r);
- },
- });
- }
- public static function findIdentityByAccessToken($token, $type = null)
- {
- return static::findOne(['auth_key' => $token]);
- }
- SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0
- https://github.com/yiisoft/yii2/issues/6631
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement