• 20-05-2020, 12:56:45
    #1
    Merhabalar,
    Symfony de bir rest api ile bağlantı kurup gerekli verileri çekmem gerekiyor. Rest api sonlanma zamanı 12 saat gibi bir süre.
    Bu api'nin token'i nerede saklayabilirim ki sürekli token isteği atmak zorunda kalmamalıyım?
    Session time 1 saat gibi bir süre.
    Veritabanı'na kaydedip oradan çekmek hiç profesyonel gelmedi açıkcası.
    Bu süreyi daha da nasıl uzun tutabilirim?

    Edit: Her user için farklı token tutacağım.
  • 20-05-2020, 13:11:41
    #2
    ini, env yada doğrudan txt gibi key=value tarzı statik bir dosyada tutulabilir. güvenlik için dosyaya doğrudan erişim apache yada nginx konfigrasyonlarıyla engellenebilir

    aşağıda kod örneği vereceğim bu örneği kendine göre düzenleyebilirsin
    checkTokens methodu içerisindeki 7200 yazan kısmı token exp süresi ne ise ona göre revize edebilirsin.
    request methodunuda yine kendine göre revize edersin hatta bu methodu sadece token oluşturma değil apiye atılacak tüm istekler için kullanacak hal getirebilirsin.

    örnek:
    <?phpclass Request{    public $BASE_URL = 'https://localhost/api/';    public $config;    public $access_token;    public $file = 'token.ini';    public function __construct($config)    {        $this->config = $config;        if (function_exists('storage_path')) {            $this->file = './token.ini';        }        $this->checkTokens();    }    public function checkTokens()    {        try {            $tokens = parse_ini_file($this->file);        } catch (Exception $e) {            @unlink($this->file);        }        if (!isset($tokens['access_token']) || !isset($tokens['created_at'])) {            return $this->authorize();        }        if (time() - (int)$tokens['created_at'] > 7200) {            return $this->authorize();        }        $this->access_token = $tokens['access_token'];        return $tokens;    }    public function authorize()    {        $resp = $this->authWithPassword();        if (isset($resp['access_token'])) {            $token = '';            foreach ($resp as $key => $value) {                $token .= $key . '=' . $value . "n";            }            file_put_contents($this->file, $token);            $this->access_token = $resp['access_token'];        }        return false;    }    public function request($path, $params = null, $method = 'POST')    {        //burada apiye istek atar apiden gelen veriyi işlersin    }    private function authWithPassword()    {        $path = $this->BASE_URL . '/access_token';        return $this->request($path, $this->config, 'POST');    }}
  • 20-05-2020, 13:36:38
    #3
    CodeS adlı üyeden alıntı: mesajı görüntüle
    ini, env yada doğrudan txt gibi key=value tarzı statik bir dosyada tutulabilir. güvenlik için dosyaya doğrudan erişim apache yada nginx konfigrasyonlarıyla engellenebilir

    aşağıda kod örneği vereceğim bu örneği kendine göre düzenleyebilirsin
    checkTokens methodu içerisindeki 7200 yazan kısmı token exp süresi ne ise ona göre revize edebilirsin.
    request methodunuda yine kendine göre revize edersin hatta bu methodu sadece token oluşturma değil apiye atılacak tüm istekler için kullanacak hal getirebilirsin.

    örnek:
    <?phpclass Request{    public $BASE_URL = 'https://localhost/api/';    public $config;    public $access_token;    public $file = 'token.ini';    public function __construct($config)    {        $this->config = $config;        if (function_exists('storage_path')) {            $this->file = './token.ini';        }        $this->checkTokens();    }    public function checkTokens()    {        try {            $tokens = parse_ini_file($this->file);        } catch (Exception $e) {            @unlink($this->file);        }        if (!isset($tokens['access_token']) || !isset($tokens['created_at'])) {            return $this->authorize();        }        if (time() - (int)$tokens['created_at'] > 7200) {            return $this->authorize();        }        $this->access_token = $tokens['access_token'];        return $tokens;    }    public function authorize()    {        $resp = $this->authWithPassword();        if (isset($resp['access_token'])) {            $token = '';            foreach ($resp as $key => $value) {                $token .= $key . '=' . $value . "n";            }            file_put_contents($this->file, $token);            $this->access_token = $resp['access_token'];        }        return false;    }    public function request($path, $params = null, $method = 'POST')    {        //burada apiye istek atar apiden gelen veriyi işlersin    }    private function authWithPassword()    {        $path = $this->BASE_URL . '/access_token';        return $this->request($path, $this->config, 'POST');    }}
    Çok teşekkürler, gayet açıklayıcı olmuş.
    Ben bunu ön muhasebe projemde e-fatura entegratör bağlantısında kullanmayı düşünüyorum. Ama sizin örnek verdiğiniz mantık ile her bir user'a txt oluştursak olabilir sanırım.
  • 20-05-2020, 17:06:46
    #4
    ssusar adlı üyeden alıntı: mesajı görüntüle
    Çok teşekkürler, gayet açıklayıcı olmuş.
    Ben bunu ön muhasebe projemde e-fatura entegratör bağlantısında kullanmayı düşünüyorum. Ama sizin örnek verdiğiniz mantık ile her bir user'a txt oluştursak olabilir sanırım.
    aslında bu tokenlar ciddi güvenlik önlemine ihtiyaç duyar ben tekil olacağını düşündüm fakat birdenf azla kullanıcı birden fazla api yani manytomany ise veritabanında tutmakta bir tercih

    user_id(int), entegration(varchar), token_data(text yada json)

    kod örneğindeki birebir mantıkla ilerlenebilir sadece veriyi ini dosyasından değil veritabanından okur fakat dosyaya yazıyorsan kesinlikle dosyalara harici erişimleri engellemelisin. zira entegre edeceğin apiler müşterilerinizin muhasebe gibi önemli arçaalra erişimini sağlıyor.